【问题标题】:Python selenium webdriver click functionalityPython selenium webdriver 点击功能
【发布时间】:2011-10-03 21:41:43
【问题描述】:

有谁知道如何使用 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 p>

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

任何帮助将不胜感激。

【问题讨论】:

    标签: python selenium click webdriver


    【解决方案1】:

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

    【讨论】:

    • 谢谢。我认为这将有很大帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多