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

时间:2017-02-19 21:25:32

标签: python selenium web-scraping phantomjs

我正在尝试使用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 "webScrap_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

我在这里缺少什么?

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

解决!

我仍然无法通过标题点击该元素,但我找到了一种解决方法,通过其“&#39;”标题来查找要点击的元素,而不是&#39;标题&#39;。

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可以&#34;看到&#34;钥匙:

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

感谢您的帮助!