python selenium点击按钮

AJW*_*AJW 65 python selenium click onclick selenium-webdriver

我是python selenium的新手,我试图点击一个具有以下html结构的按钮:

<div class="b_div">

    <div class="button c_button s_button" onclick="submitForm('mTF')">
        <input class="very_small" type="button"></input>
        <div class="s_image"></div>
        <span>
           Search
        </span>
    </div>

    <div class="button c_button s_button" onclick="submitForm('rMTF')" style="margin-bottom: 30px;">
        <input class="v_small" type="button"></input>
        <span>
              Reset
        </span>
   </div>

</div>
Run Code Online (Sandbox Code Playgroud)

我希望能够点击上面的SearchReset按钮(显然是单独的).

我尝试了几件事,例如:

driver.find_element_by_css_selector('.button .c_button .s_button').click()
Run Code Online (Sandbox Code Playgroud)

要么,

driver.find_element_by_name('s_image').click()
Run Code Online (Sandbox Code Playgroud)

要么,

driver.find_element_by_class_name('s_image').click()
Run Code Online (Sandbox Code Playgroud)

但是,我似乎总是最终得到NoSuchElementException,例如:

selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"name","selector":"s_image"}' ;
Run Code Online (Sandbox Code Playgroud)

我想知道我是否能以某种方式使用HTML的onclick属性来进行selenium点击?

任何可以指向正确方向的想法都会很棒.谢谢.

fal*_*tru 82

删除css选择器中类之间的空格:

driver.find_element_by_css_selector('.button .c_button .s_button').click()
#                                           ^         ^
Run Code Online (Sandbox Code Playgroud)

=>

driver.find_element_by_css_selector('.button.c_button.s_button').click()
Run Code Online (Sandbox Code Playgroud)

  • 我已经尝试过你所建议的。我收到相同的“NoSuchElementException”错误! (2认同)
  • @AJW,试试`print(driver.page_source)`,并检查html实际上是否包含该元素. (2认同)
  • @AJW,如何使用xpath: `driver.find_element_by_xpath('.//div[@class="button c_button s_button"][contains(., "Search")]')` (2认同)
  • @MortezaLSC,如果你的意思是在没有 GUI 的系统中这是可能的,那就是可能的。使用无头浏览器。例如,PhantomJS。 (2认同)

小智 27

对于 python,使用

from selenium.webdriver import ActionChains
Run Code Online (Sandbox Code Playgroud)

ActionChains(browser).click(element).perform()
Run Code Online (Sandbox Code Playgroud)

  • 什么是‘元素’? (5认同)

Car*_*585 22

试试这个:

下载firefox,添加插件"firebug"和"firepath"; 安装完成后,转到您的网页,启动firebug并找到元素的xpath,它在页面中是唯一的,这样您就不会犯任何错误.

见图: 指令

browser.find_element_by_xpath('只需复制并粘贴Xpath').click()

  • 非常感谢你这么棒的生活.它节省了很多时间 (3认同)
  • 您可以使用以下方法在 Firefox 上找到该元素:Tools-&gt;Web Developer-&gt;Inspector;单击 GUI 上的按钮,在检查器部分,用鼠标右键单击相关代码-&gt; 复制并选择:CSS Selector / CSS Path / Xpath ... (2认同)

Cos*_*oCD 6

我在使用 Phantomjs 作为浏览器时遇到了同样的问题,所以我通过以下方式解决了:

driver.find_element_by_css_selector('div.button.c_button.s_button').click()
Run Code Online (Sandbox Code Playgroud)

基本上我已经将 DIV 标签的名称添加到引用中。


Tan*_*nel 5

以下调试过程帮助我解决了类似的问题。

with open("output_init.txt", "w") as text_file:
    text_file.write(driver.page_source.encode('ascii','ignore'))


xpath1 = "the xpath of the link you want to click on"
destination_page_link = driver.find_element_by_xpath(xpath1)
destination_page_link.click()


with open("output_dest.txt", "w") as text_file:
    text_file.write(driver.page_source.encode('ascii','ignore'))
Run Code Online (Sandbox Code Playgroud)

然后,您应该有两个文本文件,其中包含您所在的初始页面(“output_init.txt”)和单击按钮后转发到的页面(“output_dest.txt”)。如果它们相同,那么是的,您的代码不起作用。如果不是,那么您的代码可以工作,但您还有另一个问题。对我来说,问题似乎是转换内容以生成我的钩子所需的 JavaScript 尚未执行。

我认为你的选择:

  1. 让驱动程序执行 javascript,然后调用您的查找元素代码。在 stackoverflow 上寻找更详细的答案,因为我没有遵循这种方法。
  2. 只需在“output_dest.txt”上找到一个类似的钩子即可产生相同的结果,这就是我所做的。
  3. 尝试在单击任何内容之前稍等一下:

xpath2 =“您要点击的xpath”

WebDriverWait(驱动程序,超时=5).until(lambda x: x.find_element_by_xpath(xpath2))

xpath 方法不一定更好,我只是更喜欢它,您也可以使用选择器方法。


Raj*_*ngh 5

打开一个网站https://adviserinfo.sec.gov/compilation并单击按钮下载文件,如果它使用 python selenium,我什至想关闭弹出窗口

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.chrome.options import Options 

#For Mac - If you use windows change the chromedriver location
chrome_path = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(chrome_path)

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-popup-blocking")

driver.maximize_window()
driver.get("https://adviserinfo.sec.gov/compilation")

# driver.get("https://adviserinfo.sec.gov/")
# tabName = driver.find_element_by_link_text("Investment Adviser Data")
# tabName.click()

time.sleep(3)

# report1 = driver.find_element_by_xpath("//div[@class='compilation-container ng-scope layout-column flex']//div[1]//div[1]//div[1]//div[2]//button[1]")

report1 = driver.find_element_by_xpath("//button[@analytics-label='IAPD - SEC Investment Adviser Report (GZIP)']")

# print(report1)
report1.click()

time.sleep(5)

driver.close()
Run Code Online (Sandbox Code Playgroud)