gpt4 book ai didi

Python selenium webdriver 点击功能

转载 作者:太空狗 更新时间:2023-10-30 02:34:42 24 4
gpt4 key购买 nike

有谁知道如何使用 python webdriver 绑定(bind)单击基于 href 的链接。在 waitr-webdriver 中,您可以执行如下操作:

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

但是我没能在 python webdriver 中找到类似的功能。一切尽在

通过类名查找元素
通过_css_selector 查找元素
通过id查找元素
通过链接文本查找元素
通过名称查找元素
通过_partial_link_text 查找元素
通过标签名称查找元素
通过 xpath 查找元素

这些都是很棒的方法,但我正在测试的网站在其链接中没有 ID 或类。所以关于链接的唯一独特之处是 href url。

如有任何帮助,我们将不胜感激。

最佳答案

在幕后,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']")

关于Python selenium webdriver 点击功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6669352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com