簡體   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