使用Selenium和Python单击一个按钮 [英] Click a button using Selenium and Python
本文介绍了使用Selenium和Python单击一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
<a class="sectionname" href="#" onclick="expandAll();return false;">Expand all</a>
当我单击全部展开时,将加载整个页面.如何使用WebDriver for Python做到这一点?
When I click on expand all, the whole page loads. How can I do it using WebDriver for Python?
推荐答案
根据 HTML ,您可以使用find_element_by_link_text
并调用click()
方法,如下所示:
As per the HTML you can use the find_element_by_link_text
and invoke click()
method as follows :
driver.find_element_by_link_text("Expand all").click()
您可以通过find_element_by_xpath
获得更多的词汇,如下所示:
You can get more granualar with find_element_by_xpath
as follows :
driver.find_element_by_xpath("//a[@class='sectionname' and contains(.,'Expand all')]").click()
更新
由于您仍然看不到扩展,因此可以尝试使用 Javascript 方法,如下所示:
Update
As you still don't see the expansion you can try the Javascript way as follows :
myElement = driver.find_element_by_xpath("//a[@class='sectionname' and contains(.,'Expand all')]")
driver.execute_script("arguments[0].click();", myElement)
这篇关于使用Selenium和Python单击一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文