繁体   English   中英

Python Selenium - 按标题查找和单击元素

[英]Python Selenium - Find and click element by title

我正在尝试登录一个网站以使用 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

我在这里缺少什么?

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

每次打开网站时,这些按钮的标题都会更改,因此您无法使用它来定位按钮。 我建议您使用部分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()
#...

解决了!

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

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()

感谢所有的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM