python selenium 点击按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21350605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python selenium click on button
提问by AJW
I am quite new to python selenium and I am trying to click on a button which has the following html structure:
我对 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>
I would like to be able to click both the Searchand Resetbuttons above (obviously individually).
我希望能够同时单击上面的Search和Reset按钮(显然是单独的)。
I have tried a couple of things, for example:
我尝试了几件事,例如:
driver.find_element_by_css_selector('.button .c_button .s_button').click()
or,
或者,
driver.find_element_by_name('s_image').click()
or,
或者,
driver.find_element_by_class_name('s_image').click()
but, I seem to always end up with NoSuchElementException, for example:
但是,我似乎总是以 结束NoSuchElementException,例如:
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"name","selector":"s_image"}' ;
I am wondering if I can somehow use the onclick attributes of the HTML to make selenium click?
我想知道是否可以以某种方式使用 HTML 的 onclick 属性来使 selenium 单击?
Any thoughts which can point me in the right direction would be great. Thanks.
任何可以为我指明正确方向的想法都会很棒。谢谢。
采纳答案by AshishGalagali
For python, use the
对于 python,使用
from selenium.webdriver import ActionChains
and
和
ActionChains(browser).click(element).perform()
回答by falsetru
Remove space between classes in css selector:
在 css 选择器中删除类之间的空间:
driver.find_element_by_css_selector('.button .c_button .s_button').click()
# ^ ^
=>
=>
driver.find_element_by_css_selector('.button.c_button.s_button').click()
回答by Carlo 1585
try this:
尝试这个:
download firefox, add the plugin "firebug" and "firepath"; after install them go to your webpage, start firebug and find the xpath of the element, it unique in the page so you can't make any mistake.
下载firefox,添加插件“firebug”和“firepath”;安装它们后,转到您的网页,启动 firebug 并找到元素的 xpath,它在页面中是唯一的,因此您不会犯任何错误。
browser.find_element_by_xpath('just copy and paste the Xpath').click()
browser.find_element_by_xpath('just copy and paste the Xpath').click()
回答by CosimoCD
I had the same problem using Phantomjs as browser, so I solved in the following way:
我在使用 Phantomjs 作为浏览器时遇到了同样的问题,所以我通过以下方式解决了:
driver.find_element_by_css_selector('div.button.c_button.s_button').click()
Essentially I have added the name of the DIV tag into the quote.
基本上我已经将 DIV 标签的名称添加到引用中。
回答by Tanel
The following debugging process helped me solve a similar issue.
以下调试过程帮助我解决了类似的问题。
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'))
You should then have two textfiles with the initial page you were on ('output_init.txt') and the page you were forwarded to after clicking the button ('output_dest.txt'). If they're the same, then yup, your code did not work. If they aren't, then your code worked, but you have another issue. The issue for me seemed to be that the necessary javascript that transformed the content to produce my hook was not yet executed.
然后,您应该有两个文本文件,其中包含您所在的初始页面 ('output_init.txt') 和单击按钮后转发到的页面 ('output_dest.txt')。如果它们相同,那么是的,您的代码不起作用。如果它们不是,那么您的代码可以工作,但您还有另一个问题。我的问题似乎是转换内容以生成我的钩子的必要 javascript 尚未执行。
Your options as I see it:
我所看到的你的选择:
- Have the driver execute the javascript and then call your find element code. Look for more detailed answers on this on stackoverflow, as I didn't follow this approach.
- Just find a comparable hook on the 'output_dest.txt' that will produce the same result, which is what I did.
- Try waiting a bit before clicking anything:
- 让驱动程序执行 javascript,然后调用您的查找元素代码。在 stackoverflow 上寻找更详细的答案,因为我没有遵循这种方法。
- 只需在 'output_dest.txt' 上找到一个类似的钩子,它会产生相同的结果,这就是我所做的。
- 在单击任何内容之前尝试等待一下:
xpath2 = "your xpath that you are going to click on"
WebDriverWait(driver, timeout=5).until(lambda x: x.find_element_by_xpath(xpath2))
xpath2 = "你要点击的 xpath"
WebDriverWait(driver, timeout=5).until(lambda x: x.find_element_by_xpath(xpath2))
The xpath approach isn't necessarily better, I just prefer it, you can also use your selector approach.
xpath 方法不一定更好,我只是更喜欢它,您也可以使用选择器方法。


