Python 选择器无效:xpath表达式“的结果”//*[@id=';topstuff';]/div/div/p[1]/text()[2]";is:[对象文本]使用XPath和Selenium
我在理解如何做到这一点上遇到了很多问题。我需要做的很简单,就是在我的自动谷歌搜索无法找到任何搜索结果时进行标记。我的代码示例:Python 选择器无效:xpath表达式“的结果”//*[@id=';topstuff';]/div/div/p[1]/text()[2]";is:[对象文本]使用XPath和Selenium,python,selenium,selenium-webdriver,xpath,xpath-1.0,xpath-2.0,Python,Selenium,Selenium Webdriver,Xpath,Xpath 1.0,Xpath 2.0,我在理解如何做到这一点上遇到了很多问题。我需要做的很简单,就是在我的自动谷歌搜索无法找到任何搜索结果时进行标记。我的代码示例: driver = webdriver.Chrome(executable_path) driver.get("https://google.com/") search = driver.find_element_by_name("q") search.send_keys('site:'+'www.pa.gov'+ ' "
driver = webdriver.Chrome(executable_path)
driver.get("https://google.com/")
search = driver.find_element_by_name("q")
search.send_keys('site:'+'www.pa.gov'+ ' "ADT.com" '+'\n')
if driver.find_element(By.XPATH, "//*[@id='topstuff']/div/div/p[1]/text()[2]"):
print(True)
else:
print(False)
我不断地发现这个错误:
InvalidSelectorException: invalid selector: The result of the xpath expression "//*[@id='topstuff']/div/div/p[1]/text()[2]" is: [object Text]. It should be an element.
(Session info: chrome=87.0.4280.88)
这是我搜索过的链接
我做错了什么?此错误消息
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//a[following-sibling::input[@value="ST"]]/@href" is: [object Attr]. It should be an element.
……表示您的XPath表达式不是有效的XPath表达式
您需要替换:
driver.find_element(By.XPATH, "//*[@id='topstuff']/div/div/p[1]/text()[2]")
与:
其他考虑事项 仅支持xpath-1.0,它返回xpath选择的节点集 您可以在中找到规格 但是,xpath表达式:
driver.find_elements(By.XPATH, "//*[@id='topstuff']/div/div/p[1]/text()")[2]
是基于xpath-2.0的表达式,通常会返回对象文本
。举几个例子:
:选择与上下文节点位于同一文档中的所有版本属性节点/@version
:选择上下文节点父节点的lang属性。/@lang
解决方案 因此,有效的代码块将是:
if driver.find_elements(By.XPATH, "//*[@id='topstuff']/div/div/p[1]")[2]:
此错误消息
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//a[following-sibling::input[@value="ST"]]/@href" is: [object Attr]. It should be an element.
……表示您的XPath表达式不是有效的XPath表达式
您需要替换:
driver.find_element(By.XPATH, "//*[@id='topstuff']/div/div/p[1]/text()[2]")
与:
其他考虑事项 仅支持xpath-1.0,它返回xpath选择的节点集 您可以在中找到规格 但是,xpath表达式:
driver.find_elements(By.XPATH, "//*[@id='topstuff']/div/div/p[1]/text()")[2]
是基于xpath-2.0的表达式,通常会返回对象文本
。举几个例子:
:选择与上下文节点位于同一文档中的所有版本属性节点/@version
:选择上下文节点父节点的lang属性。/@lang
解决方案 因此,有效的代码块将是:
if driver.find_elements(By.XPATH, "//*[@id='topstuff']/div/div/p[1]")[2]:
selenium还不支持您提供的Xpath 我相信你捕捉错误的网站错误 诱导
WebDriverWait
(),并等待位于()和xpath后面的元素的可见性
如果发生任何错误,则诱导try..except
块进行处理
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Chrome(executable_path)
driver.get("https://google.com/")
search = driver.find_element_by_name("q")
search.send_keys('site:'+'www.pa.gov'+ ' "ADT.com" '+'\n')
try:
Searchelement=WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='topstuff']//p[@role='heading']")))
print(True)
print("============================")
print(Searchelement.text)
print("============================")
#If you want to get specific node value then try below xpath
print(driver.find_element_by_xpath("//div[@id='topstuff']//p[@role='heading']/span/em").text)
except:
print(False)
控制台输出:
True
============================
Your search - site:www.pa.gov "ADT.com" - did not match any documents.
============================
site:www.pa.gov "ADT.com"
selenium还不支持您提供的Xpath
我相信你捕捉错误的网站错误
诱导WebDriverWait
(),并等待位于()和xpath后面的元素的可见性
如果发生任何错误,则诱导try..except
块进行处理
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Chrome(executable_path)
driver.get("https://google.com/")
search = driver.find_element_by_name("q")
search.send_keys('site:'+'www.pa.gov'+ ' "ADT.com" '+'\n')
try:
Searchelement=WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='topstuff']//p[@role='heading']")))
print(True)
print("============================")
print(Searchelement.text)
print("============================")
#If you want to get specific node value then try below xpath
print(driver.find_element_by_xpath("//div[@id='topstuff']//p[@role='heading']/span/em").text)
except:
print(False)
控制台输出:
True
============================
Your search - site:www.pa.gov "ADT.com" - did not match any documents.
============================
site:www.pa.gov "ADT.com"
我现在遇到这个错误:a=driver.find_元素(By.XPATH,“/*[@id='topstuff']/div/div/p[1]”[2]索引器:列出索引range@NateLeMay您尚未向我们提供元素的HTML。因此,无法进一步帮助您使用索引器:列表索引超出范围
,因为我们无法假设有多少元素被识别并放置在列表中,但此解决方案肯定解决了您最初的InvalidSelectorException错误:invalid selectorSelenium不支持节点text()
@KunLun在我的有效/建议解决方案中,您在哪里找到了text()
。@KunLun是否愿意根据您的上述评论回答我的反问题?在我的有效/建议的解决方案中,您在哪里找到了text()
?我现在遇到了这个错误:a=driver.find_元素(By.XPATH,“/*[@id='topstuff']]/div/div/p[1]”[2]索引器:列出索引range@NateLeMay您尚未向我们提供元素的HTML。因此,无法进一步帮助您使用索引器:列表索引超出范围
,因为我们无法假设有多少元素被识别并放置在列表中,但此解决方案肯定解决了您最初的InvalidSelectorException错误:invalid selectorSelenium不支持节点text()
@KunLun在我的有效/建议解决方案中,您在哪里找到了text()
。@KunLun是否愿意根据您的上述评论回答我的反问题?在我的有效/建议的解决方案中,您在哪里找到了text()
?Selenium不支持text()
节点。您需要在/*[@id='topstuff']/div/div/p[1]
处停止并获取整个文本。在提取完所需的文本后。或者查找另一个未使用text()
作为节点的xpath
。Selenium不支持text()
节点。您需要在/*[@id='topstuff']/div/div/p[1]
处停止并获取整个文本。在提取完所需的文本后。或者找到另一个不使用text()
作为节点的xpath
。我认为web scraping OP需要了解并解决InvalidSelectorException
。报废也没什么大不了的。我认为web报废OP需要了解并解决InvalidSelectorException
。报废也没什么大不了的。