我正在嘗試登錄我的大學網站,但收到此錯誤:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div[2]/div/div/div/div[2]/form/table/tbody/tr[3]/td[2]/input"}
這是代碼部分:
textboxes3 = browser.find_element_by_xpath("/html/body/div/div/div[2]/div/div/div/div[2]/form/table/tbody/tr[3]/td[2]/input")
textboxes3.click()
textboxes3.send_keys(matricola)
這是現場檢查:1
uj5u.com熱心網友回復:
嘗試按 id 定位元素:
textboxes3 = browser.find_element_by_id("username")
textboxes3.click()
textboxes3.send_keys(matricola)
uj5u.com熱心網友回復:
使用 browser.find_element_by_id("username") 應該可以作業,但由于它仍然沒有找到元素,所以我懷疑可能會發生以下兩個問題之一:
- 元素尚未加載,硒需要等待更長時間。我建議使用 WebDriverWait 類來等待元素加載。
- 網站分為 iframe,Selenium 可能在錯誤的 iframe 中搜索元素。如果是這種情況,您將需要使用 driver.switch_to.frame 函式。
我建議先試試這個代碼:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
w = WebDriverWait(self.driver, 10)
textboxes3 = w.until(ec.presence_of_element_located((By.ID, 'username')))
textboxes3.click()
textboxes3.send_keys('matricola')
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/391983.html
