【问题标题】:How to click on the element as per the HTML provided through selenium-webdriver and Python如何根据 selenium-webdriver 和 Python 提供的 HTML 单击元素
【发布时间】:2019-02-15 19:19:00
【问题描述】:

您好,我尝试在 python 中的 selenium 驱动程序的帮助下单击单选按钮,但它不起作用。这是 HTML 代码:

<input aria-flowto="aria8" aria-label="private key" type="radio" ng-model="walletType" value="pasteprivkey" class="ng-pristine ng-valid ng-empty ng-touched" name="200">

这是我的代码行:

browser.find_elements_by_xpath("input[type='radio'][value='pasteprivkey']").click()

我得到了这个错误:

DevTools listening on ws://127.0.0.1:52666/devtools/browser/da96711c-0446-c01-a90d-0f722691ec4c
Traceback (most recent call last):
  File "C:\Users\Andrei\Desktop\py\teste.py", line 6, in <module>
    browser.find_element_by_xpath("//*[@type='radio'][@value='pasteprivkey']").click()
  File "C:\Users\Andrei\AppData\Local\Programs\Python\Python37\lib\site-packages
\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\Andrei\AppData\Local\Programs\Python\Python37\lib\site-packages
\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Andrei\AppData\Local\Programs\Python\Python37\lib\site-packages
\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Andrei\AppData\Local\Programs\Python\Python37\lib\site-packages
\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=68.0.3440.106)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.1.7601 SP1 x86_64)

有人可以帮助我吗?我还尝试使用检查元素复制 xpath,但我得到了这个奇怪的东西:

/html/body/section[1]/div[1]/main/article[1]/div[2]/wallet-decrypt-drtv/article/section[1]/label[9]/input

【问题讨论】:

标签: python selenium xpath css-selectors webdriver


【解决方案1】:

错误消息表明该元素不可见。

您的选择器 IS 找到了输入元素,但是在尝试单击时,Selenium 报告它不可见。 Selenium 不允许在完全隐藏或位于其他元素后面的元素上发生点击事件。

如果无法访问您的网页,就无法理解为什么此元素不可见。您可以尝试以下方法之一;

  • 了解显示元素的步骤,并在脚本中执行这些步骤
  • 找到应该与之交互的其他元素
  • 使用 Javascript 执行点击(注意,如果您使用 Selenium 进行测试,我不建议这样做)

终于;

我也尝试使用检查元素复制 xpath,但我得到了这个奇怪的东西

这是一个 XPath,也是您的浏览器建议的。应该记住,XPath 不是一个确定的东西。一个元素可以被很多很多 XPath 找到。这些 XPath 中有些是好的,有些是坏的。选择最合适的 XPath 需要自动化的经验和知识,正如您所见,这很难通过工具来实现。

【讨论】:

    【解决方案2】:

    根据 HTML,您共享的所需元素是 Angular 元素,因此要在其上调用 click(),您必须为 WebDriverWait em>元素可点击,您可以使用以下任一解决方案:

    • CSS_SELECTOR:

      WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ng-pristine.ng-valid.ng-empty.ng-touched[ng-model='walletType'][value='pasteprivkey']"))).click()
      
    • XPATH:

      WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-valid ng-empty ng-touched' and @ng-model='walletType'][@value='pasteprivkey']"))).click()
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    【讨论】:

    • 没有为您编写的两行代码定义名称驱动程序(对于 xpath 和 css )。我还尝试用我的变量 browser=webdriver.Chrome() 替换该驱动程序,但它不起作用
    • @Theminer 查看我更新的答案并让我知道状态
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多