【问题标题】:python selenium get button by class and clickpython selenium 按类获取按钮并单击
【发布时间】:2021-05-31 14:57:55
【问题描述】:

我在 Python 中使用 selenium。该页面有一个带有与之关联的类的按钮。我按类使用了 get 元素,但它失败了。请检查下面的代码

<div class="form-cell">
    <button type="submit" class="btn btn-orange">Sign Up</button>
</div>

到目前为止已经尝试了以下...并且每次都出现相同的错误。在实际代码中,前三行被注释,所以请注意。

driver.find_element_by_class_name('btn btn-orange').click()
driver.find_elements_by_class_name('btn btn-orange').click()
driver.find_element_by_xpath('//button[@type="button"]/[@class="btn btn-orange"]').click()
driver.find_element_by_xpath("//button[@type='button']/option[text()='Sign Up']").click()

对于上述所有尝试,这都是相同的错误消息。

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@type='button']/option[text()='Sign Up']"}
  (Session info: chrome=91.0.4472.77)

我已设法识别文本框并将某些数据输入其中。但现在我正在尝试提交该数据,但似乎无法抓住按钮来点击它。

感谢您的帮助

【问题讨论】:

  • 能否提供网页链接?

标签: python selenium button xpath


【解决方案1】:

试试这个:

driver.find_element_by_css_selector('.btn.btn-orange').click()

find_element_by_class_name() 没有正确处理类名中的空格。

如果你想使用 XPath,我认为应该是这样的

driver.find_element_by_xpath('//button[@class="btn btn-orange"]').click()

【讨论】:

    【解决方案2】:

    试试这个:

    driver.find_element_by_xpath('//button[@type="submit" and (contains(@class,"btn-orange")) and(contains(text(),"Sign Up"))]').click()
    

    这应该 100% 抓住它;)

    【讨论】:

      【解决方案3】:

      试试这个 xpath:

      .//button[@type='submit' and text()='Sign Up']
      

      在您共享的 html 中没有“按钮”类型和选项文本

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-18
        • 1970-01-01
        • 2021-10-18
        • 1970-01-01
        • 2022-01-05
        • 2020-11-28
        • 2020-07-28
        • 1970-01-01
        相关资源
        最近更新 更多