我是 python 和 selenium 的新手,我寫了一個代碼,在其中行得到錯誤Message: no such element: Unable to locate element。即使它存在。
代碼段:-
driver.find_element(By.XPATH, "//*[@id='react-joyride-step-0']/div/div/div/div[2]/div/button").click()
完整的錯誤日志:-
DevTools listening on ws://127.0.0.1:57704/devtools/browser/2ecf6fa2-0489-46a9-b304-95cc40b4c2cc
Convin
logged in
Traceback (most recent call last):
File "C:\Users\Lenovo\Desktop\convin\automation_test\logintest.py", line 38, in <module>
driver.find_element(By.XPATH, "//*[@id='react-joyride-step-0']/div/div/div/div[2]/div/button").click()
File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1238, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
self.error_handler.check_response(response)
File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\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":"xpath","selector":"//*[@id='react-joyride-step-0']/div/div/div/div[2]/div
/button"}
(Session info: chrome=95.0.4638.69)
Stacktrace:
Backtrace:
Ordinal0 [0x00780C43 2493507]
Ordinal0 [0x0071A4B1 2073777]
Ordinal0 [0x00622608 1058312]
Ordinal0 [0x0064CAA4 1231524]
Ordinal0 [0x00676C62 1404002]
Ordinal0 [0x0066597A 1333626]
Ordinal0 [0x00675038 1396792]
Ordinal0 [0x0066580B 1333259]
Ordinal0 [0x00642314 1188628]
Ordinal0 [0x0064316F 1192303]
GetHandleVerifier [0x00907BF6 1548950]
GetHandleVerifier [0x009B461C 2256060]
GetHandleVerifier [0x0080C13B 518107]
GetHandleVerifier [0x0080B1E0 514176]
Ordinal0 [0x0071F53D 2094397]
Ordinal0 [0x00723418 2110488]
Ordinal0 [0x00723552 2110802]
Ordinal0 [0x0072CE81 2150017]
BaseThreadInitThunk [0x7602FA29 25]
RtlGetAppContainerNamedObjectPath [0x77467A9E 286]
RtlGetAppContainerNamedObjectPath [0x77467A6E 238]
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C4800:29492:1123/114516.398:ERROR:chrome_browser_main_extra_parts_metrics.cc(230)] crbug.com/1216328: Checking Bluetooth availability s
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C ends.
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^Cn_extra_parts_metrics.cc(233)] crbug.com/1216328: Checking Bluetooth availability ended.
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^Cmpl.cc(214)] [11:45:16.398] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to th
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^Cn_extra_parts_metrics.cc(236)] crbug.com/1216328: Checking default browser status started. Please report if there is no report that th
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^Cmpl.cc(214)] [11:45:16.400] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to th
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^Cn_extra_parts_metrics.cc(240)] crbug.com/1216328: Checking default browser status ended.
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C0360:29660:1123/114708.812:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is disabled, ANGLE is
開發者控制臺截圖:-

提前致謝。希望很快就到這里。我真的卡住了請幫忙。
uj5u.com熱心網友回復:
要單擊帶有文本的元素作為跳過,您可以使用以下任一定位器策略:
使用css:
driver.find_element(By.CSS_SELECTOR, "button[title='Skip'][aria-label='Skip'][data-action='skip']").click()使用xpath:
driver.find_element(By.XPATH, "//button[@title='Skip' and @aria-label='Skip'][@data-action='skip']").click()
理想的情況下到上點擊可點擊元素,你需要引起WebDriverWait的element_to_be_clickable(),你可以使用以下的定位策略:
使用CSS:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Skip'][aria-label='Skip'][data-action='skip']"))).click()使用XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@title='Skip' and @aria-label='Skip'][@data-action='skip']"))).click()注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
uj5u.com熱心網友回復:
這對我有用:
skip = driver.find_element(By.XPATH, "//*[@id='react-joyride-step-0']/div/div/div/div[2]/div/button")
driver.execute_script("arguments[0].click();", skip)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/363761.html
上一篇:我們可以使用seleniumUI回歸腳本進行性能測驗嗎
下一篇:從硒VBA中的日歷中選擇日期
