Python Selenium Webdriver的点击功能

4

有没有人知道如何使用Python Webdriver绑定根据href点击链接。在waitr-webdriver中,您可以执行以下操作:

browser.link(:href => "http://www.testsite.com/pageOne.html").click

但是我在Python的Webdriver中没有找到类似的功能。所有可用的方法都是:
find_element_by_class_name find_element_by_css_selector find_element_by_id
find_element_by_link_text
find_element_by_name find_element_by_partial_link_text
find_element_by_tag_name find_element_by_xpath
这些方法非常棒,但我所测试的网站链接没有ID或Class。因此,链接的唯一标识只有href URL。
希望能得到您的帮助,非常感激。
1个回答

6
在底层,watir-webdriver会将查找href属性值为指定值的链接的调用转换为WebDriver的find-by-XPath表达式。在您自己的代码中模仿这个过程,Python中处理此过程的适当方式应该是:
# assume driver is an instance of WebDriver
# NOTE: this code is untested
driver.find_element_by_xpath(".//a[@href='http://www.testsite.com/pageOne.html']")

或者,您可以使用CSS选择器(在IE上速度更快),如下所示:

# again, assume driver is an instance of WebDriver
# NOTE: this code is untested
driver.find_element_by_css_selector("a[href='http://www.testsite.com/pageOne.html']")

谢谢你。我认为这会非常有帮助。 - hackthisjay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接