如何根据通过selenium-webdriver和Python提供的HTML单击元素

问题描述 投票:-4回答:2

你好我试图在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)

有人可以帮帮我吗?我也尝试用inspect元素复制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
2个回答
1
投票

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

您的选择器正在查找输入元素,但在尝试单击时,Selenium会报告它不可见。 Selenium不允许对完全隐藏或位于其他元素后面的元素进行单击事件。

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

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

最后;

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

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


1
投票

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

  • CSS_SELECTORWebDriverWait(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()
  • XPATHWebDriverWait(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
© www.soinside.com 2019 - 2024. All rights reserved.