Selenium Python 脚本找不到存在的 XPATH

Selenium Python script won't find an XPATH that is present

提问人:Tyler Dodgen 提问时间:11/9/2023 更新时间:11/9/2023 访问量:66

问:

我正在尝试编写一个脚本,该脚本打开我的暂存 URL 并通过流单击/发送密钥。

我遇到了我认为表明该元素不存在的错误(消息:未知错误:result.webdriverValue.value list 在 Runtime.callFunctionOn 响应中缺失或为空)

我是一个菜鸟,只是想改善他们的生活,所以如果有人能诊断出我的剧本,我将不胜感激。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
import time


    chrome_driver_path = r"C:\Users\tyler\Downloads\chromedriver-win64\chromedriver-win64\chromedriver.exe"
    
    service = Service(executable_path=chrome_driver_path)
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(service=service, options=options)
    
    try:
        driver.get("my-staging-URL")
        time.sleep(10)
    
        driver.find_element("xpath", '/html/body/div[1]/div[1]/header/div[1]/div[2]/div/div[3]/div/button[2]/div/div/p').click()
        
        time.sleep(5)
      
    #driver.get("https://www.youtube.com/results?search_query=selenium+xpath")
    
        
    
    except NoSuchElementException as e:
        print("Element not found:", e)
    
    input("Press any key to close the browser")

对于更多上下文,当我在 Katalon 记录器等工具中使用此 xpath 时,它工作正常。

python-3.x selenium-webdriver 自动化

评论

0赞 GotStu 11/9/2023
你有XPATH在做任何事情吗?
0赞 Tyler Dodgen 11/9/2023
我刚刚尝试了谷歌的输入字段,它似乎不起作用
0赞 GotStu 11/9/2023
如果你是Selenium的新手,刚刚开始,我可能会建议你看看Playwright。
0赞 Tyler Dodgen 11/9/2023
可能是合理的建议,但我会坚持学习硒,我看到我提出的一个问题。xpath 应为 By.XPATH。
0赞 Potato 11/9/2023
您可能需要查看find_element方法,并确保使用正确的 Xpath(右键单击、检查并确保元素以蓝色完全突出显示)

答:

3赞 Tououououer 11/9/2023 #1

这在selenium4.1中不是正确的表达......driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/header/div[1]/div[2]/div/div[3]/div/button[2]/div/div/p').click()