首页 > 解决方案 > Selenium 无法使用 python 定位错误?

问题描述

在将 selenium im 与 python 结合使用时出现以下错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/main/article/section/form/div[1]/div[2]/div/div/div/div/div/div/input"}

我的 python 脚本如下所示:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import time 
import random
import string 


driver = webdriver.Chrome("D:\Programmieren\Python\Python\pybo\chromedriver.exe")
driver.get("https://www.langenscheidt.com/jugendwort-des-jahres")
time.sleep(10)

driver.find_element_by_xpath("/html/body/main/article/section/form/div[1]/div[2]/div/div/div/div/div/div/input")

目标html是这样的:

<input aria-labelledby="question-title-463803684" id="463803684" aria-required="true" data-sm-open-single="true" class="wds-input wds-input--lg qt-input_text text" name="463803684" size="100" value="">

标签: pythonhtmlseleniumselenium-webdriver

解决方案


所以我检查了网站,您要填写的表格包含(嵌入)html,这意味着另一对标签内有标签。我不知道如何解析,但如果您只想填写该表格,则有一个单独的 URL:

https://www.surveymonkey.com/r/7JZRVLJ

这是相同的表格,只是没有嵌入到以前的网站中。您可以将此 URL 用于您的 selenium 自动化。


推荐阅读