使用Python+Selenium自动化时,为何正确代码仍出现Traceback报错?
Selenium通过LINK_TEXT定位"Forgot password?"元素失败
问题代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By service_obj = Service("Downloads.exe") driver = webdriver.Chrome(service =service_obj) driver.get("https://rahulshettyacademy.com/client/auth/login") driver.find_element(By.LINK_TEXT,"Forgot password?").click()
报错信息
Traceback (most recent call last): File "C:\Users\938861\PycharmProjects\pythonProject\Locatorsextension.py", line 8, in <module> driver.find_element(By.LINK_TEXT,"Forgot password?").click() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\938861\PycharmProjects\pythonProject\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 861, in find_element return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\938861\PycharmProjects\pythonProject\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute self.error_handler.check_response(response) File "C:\Users\938861\PycharmProjects\pythonProject\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Forgot password?"} (Session info: chrome=107.0.5304.107) Stacktrace: Backtrace: Ordinal0 [0x0064ACD3+2075859] Ordinal0 [0x005DEE61+1633889] Ordinal0 [0x004DB7BD+571325] Ordinal0 [0x0050AC2F+764975] Ordinal0 [0x0050AE1B+765467] Ordinal0 [0x0053D0F2+970994] Ordinal0 [0x00527364+881508] Ordinal0 [0x0053B56A+963946] Ordinal0 [0x00527136+880950] Ordinal0 [0x004FFEFD+720637] Ordinal0 [0x00500F3F+724799] GetHandleVerifier [0x008FEED2+2769538] GetHandleVerifier [0x008F0D95+2711877] GetHandleVerifier [0x006DA03A+521194] GetHandleVerifier [0x006D8DA0+516432] Ordinal0 [0x005E682C+1665068] Ordinal0 [0x005EB128+1683752] Ordinal0 [0x005EB215+1683989] Ordinal0 [0x005F6484+1729668] BaseThreadInitThunk [0x75C77D69+25] RtlInitializeExceptionChain [0x7710BB9B+107] RtlClearBits [0x7710BB1F+191] Process finished with exit code 1
解决方案
1. 等待页面元素加载完成
页面未完全渲染就执行定位是常见原因,使用显式等待确保元素可交互:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC service_obj = Service("Downloads.exe") driver = webdriver.Chrome(service=service_obj) driver.get("https://rahulshettyacademy.com/client/auth/login") # 最长等待10秒,直到元素可点击 wait = WebDriverWait(driver, 10) forgot_pwd_link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Forgot password?"))) forgot_pwd_link.click()
2. 验证LINK_TEXT完全匹配
右键检查页面中的"Forgot password?"链接,确认<a>标签内的文本与代码中的字符串完全一致,包括空格、标点(如问号是否为半角)。
3. 检查元素是否在iframe内
如果链接嵌套在iframe中,需先切换到iframe再定位:
# 替换为实际iframe的定位方式(如ID、XPATH) iframe = driver.find_element(By.ID, "target-iframe-id") driver.switch_to.frame(iframe) # 定位并点击链接 driver.find_element(By.LINK_TEXT,"Forgot password?").click() # 操作完成后切回主文档 driver.switch_to.default_content()
4. 更换定位方式
若LINK_TEXT定位失败,可尝试其他更稳定的定位方式:
- PARTIAL_LINK_TEXT:匹配部分文本
driver.find_element(By.PARTIAL_LINK_TEXT, "Forgot password").click() - XPATH:精准匹配文本
driver.find_element(By.XPATH, "//a[text()='Forgot password?']").click() - CSS选择器:通过链接属性定位
driver.find_element(By.CSS_SELECTOR, "a[href*='forgot-password']").click()
内容的提问来源于stack exchange,提问作者Natasha Chouhan
相关产品推荐
相关产品推荐

