我正在嘗試學習硒。下面的代碼應該在 Chrome 中打開 fastmail 并允許我搜索指定的 web 元素。
通過逐行輸入代碼,我可以讓它在 Python 解釋器中作業。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.fastmail.com/login/")
username = driver.find_element(By.ID, 'v17-input')
但是,當我嘗試將它作為完整的 Python 腳本運行時,我無法找到我正在尋找的元素。錯誤訊息如下。
Traceback (most recent call last):
File "C:\MyPythonScripts\test.py", line 9, in <module>
username = driver.find_element(By.ID, 'v17-input')
File "C:\Users\slong\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 856, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\slong\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute
self.error_handler.check_response(response)
File "C:\Users\slong\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="v17-input"]"}
(Session info: chrome=106.0.5249.119)
Stacktrace:
Backtrace:
Ordinal0 [0x00801ED3 2236115]
Ordinal0 [0x007992F1 1807089]
Ordinal0 [0x006A66FD 812797]
Ordinal0 [0x006D55DF 1005023]
Ordinal0 [0x006D57CB 1005515]
Ordinal0 [0x00707632 1209906]
Ordinal0 [0x006F1AD4 1120980]
Ordinal0 [0x007059E2 1202658]
Ordinal0 [0x006F18A6 1120422]
Ordinal0 [0x006CA73D 960317]
Ordinal0 [0x006CB71F 964383]
GetHandleVerifier [0x00AAE7E2 2743074]
GetHandleVerifier [0x00AA08D4 2685972]
GetHandleVerifier [0x00892BAA 532202]
GetHandleVerifier [0x00891990 527568]
Ordinal0 [0x007A080C 1837068]
Ordinal0 [0x007A4CD8 1854680]
Ordinal0 [0x007A4DC5 1854917]
Ordinal0 [0x007AED64 1895780]
BaseThreadInitThunk [0x76926739 25]
RtlGetFullPathName_UEx [0x777A8FD2 1218]
RtlGetFullPathName_UEx [0x777A8F9D 1165]
我不知道它是關于什么的。該錯誤訊息似乎暗示它無法找到該元素。
uj5u.com熱心網友回復:
我在這里看到兩個問題:
- 您需要添加延遲以等待元素的可點擊性。
WebDriverWaitexpected_conditions是正確的工具。 - 您使用了錯誤的定位器。
此代碼有效:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://www.fastmail.com/login/'
driver.get(url)
wait = WebDriverWait(driver, 10)
username_field = wait.until(EC.element_to_be_clickable((By.ID, "v16-input")))
password_field = wait.until(EC.element_to_be_clickable((By.ID, "v17-input")))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/517017.html
