【问题标题】:How do I click on a button that has no ID or HTML using Python Selenium?如何使用 Python Selenium 单击没有 ID 或 HTML 的按钮?
【发布时间】:2018-03-12 07:51:30
【问题描述】:

我编写了一个脚本,并且能够为具有 html 的按钮编写代码,但我在此链接上检查的这个按钮没有。

这是登录按钮的元素。

<input type="submit" value="login" class="button">

我将如何编写这行脚本来使用 selenium 和 python 点击​​这个按钮?

【问题讨论】:

标签: javascript python python-3.x selenium


【解决方案1】:

要单击带有登录文本的按钮,您可以使用以下代码行:

browser.find_element_by_xpath("//form[@method='post' and @action='/auth/login/']//input[@class='button' and @value='login']").click()
#or
browser.find_element_by_xpath("//form[@method='post' and @action='/auth/login/']//input[@class='button' and @value='login']").submit()

【讨论】:

    【解决方案2】:

    您的元素几乎没有我们可以使用的属性。尝试查找以 input 开头并具有属性 type='submit' 的元素

    driver.find_element_by_xpath("//div[@class='content']//input[@type='submit']").click()
    

    【讨论】:

    • 请注意,有两个带有两个提交按钮的 Auth 表单 - 第一个是隐藏的,因此您的代码将不起作用!
    • 嗯,我试过了,没用。我得到这个以换取 CMD 上的字符串
    • selenium.common.exceptions.NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//div[@class='button -status-container']/button[@type='submit']"}
    • @OtakuWiz , //div[@class='button-status-container']/button[@type='submit']?但建议的 XPath 完全不同
    • @OtakuWiz 这是您在测试中遇到的另一个错误。这不适用于这部分代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多