【问题标题】:Python Selenium - Find and click element by titlePython Selenium - 按标题查找并单击元素
【发布时间】:2017-07-09 01:17:14
【问题描述】:

我正在尝试登录一个网站以使用 Selenium 抓取其内容。该网站有一个虚拟键盘,用户在其中输入密码,我想模拟在这个键盘上的点击。

https://www.rico.com.vc/

检查网站,那是键盘生成的部分(每个键的位置是由JavaScript随机生成的,但那部分是可以的):

<div class="password-buttons">
 <button class="button orange rounded" onclick="AddPsitions('8|9'); return false;" title="5 ou 3">
  <span class="login-number">5</span>
  <span class="login-or">ou</span>
  <span class="login-number">3</span>
 </button>
 <button class="button orange rounded" onclick="AddPsitions('6|7'); return false;" title="8 ou 2">
  <span class="login-number">8</span>
  <span class="login-or">ou</span>
  <span class="login-number">2</span>
 </button>
 <button class="button orange rounded" onclick="AddPsitions('4|5'); return false;" title="0 ou 6">
  <span class="login-number">0</span>
  <span class="login-or">ou</span>
  <span class="login-number">6</span>
 </button>

为了模拟点击,我正在执行以下操作(我还尝试了“//*[@title='0 ou 6']”,没有'.'):

browser.find_element_by_xpath(".//*[@title='0 ou 6']").click()

但是我收到了这个错误:

Traceback (most recent call last):
 File "webScrape_Rico.py", line 59, in <module>
 browser.find_element_by_xpath(a).click()
 File "/home/luciano/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/home/luciano/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
'value': value})['value']
File "/home/luciano/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/home/luciano/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: {" errorMessage":"Unable to locate an element with the xpath expression \".//*[@title='0 ou 6']\" because of the following error:\nError: TYPE_ERR: DOM XPath Exception 52","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"109","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:60775","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"8ddb4f90-f6e6-11e6-9eb0-4ba40f4453e7\", \"value\": \"\\\".//*[@title='0 ou 6']\\\"\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/8ddb4f90-f6e6-11e6-9eb0-4ba40f4453e7/element"}}

我在这里看到了这个选项:Find and click element by title Python Selenium

我在这里错过了什么?

【问题讨论】:

    标签: python selenium web-scraping phantomjs


    【解决方案1】:

    XPath //button[@title='7 ou 8'] 为我工作。您可以使用$x() 在 Chrome 浏览器中测试 XPath,例如$x("//button[@title='7 ou 8']")。它将帮助您更快地找到正确的定位器。

    【讨论】:

      【解决方案2】:

      每次打开网站时,这些按钮的标题都会发生变化,因此您无法使用它来定位这些按钮。我建议您将使用部分onclick 属性的所有按钮定位到列表中并使用索引来单击它们

      要确保元素在单击之前可见,您可以使用 expected_conditions 显式等待

      from selenium.webdriver.support import expected_conditions as EC
      
      wait = WebDriverWait(driver, 10)
      buttons = wait.until(EC.visibility_of_any_elements_located((By.XPATH, "//button[contains(@onclick, 'AddPsitions')]")))
      buttons[0].click()
      buttons[1].click()
      #...
      

      【讨论】:

      • 我正在处理关于每次更改按钮的问题。无论如何,我尝试了你的方法并得到了这个错误: [...] selenium.common.exceptions.InvalidElementStateException: Message: {"errorMessage":"SyntaxError: DOM Exception 12" [...]
      • 但我尝试了不同的方法:buttons = browser.find_elements_by_xpath("//button[@class='button orange rounded']"),我能够找到按钮,但现在出现此错误:selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not currently visible and may not be manipulated" 对此有何想法?
      • @lucianoaraujo 编辑了我的答案。顺便说一句,xpath "//button[@class='button orange rounded']" 没有找到其中一个按钮。
      【解决方案3】:

      解决了!

      我仍然无法通过标题单击元素,但我找到了一种解决方法,通过其“类”而不是“标题”找到要单击的元素。

      browser.find_elements_by_xpath("//button[contains(@class, 'button orange rounded')]")
      

      然后点击带有索引的元素:

      a[click].click()
      

      我也遇到了以下问题:

      selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not currently visible and may not be manipulated
      

      这是因为我没有先选择键盘。 由于 selenium 从字面上模拟用户操作,所以我首先要选择虚拟键盘,这样 selenium 才能“看到”按键:

      browser.find_element_by_id("txtPassword").click()
      

      感谢大家的帮助!

      【讨论】:

        猜你喜欢
        • 2014-10-11
        • 1970-01-01
        • 2016-02-27
        • 2018-10-12
        • 1970-01-01
        • 1970-01-01
        • 2020-07-09
        • 2023-03-27
        • 1970-01-01
        相关资源
        最近更新 更多