无效的选择器:xpath 表达式“//*[@id='topstuff']/div/div/p[1]/text()[2]”的结果是:使用 XPath 和 Selenium 的 [object Text]
Invalid selector: The result of the xpath expression "//*[@id='topstuff']/div/div/p[1]/text()[2]" is: [object Text] using XPath and Selenium
我在理解如何执行此操作时遇到了很多问题。我需要做的很简单,就是在我的自动 google 搜索找不到任何搜索结果时进行标记。我的代码示例:
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)
这是我搜索过的 link No Results
我做错了什么?
这个错误信息...
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]")
与:
driver.find_elements(By.XPATH, "//*[@id='topstuff']/div/div/p[1]/text()")[2]
其他注意事项
仅支持 xpath-1.0 returns xpath[= 选择的节点集52=].
You can find the xpath-1.0 specifications in XML Path Language (XPath) Version 1.0
但是 xpath 表达式:
driver.find_elements(By.XPATH, "//*[@id='topstuff']/div/div/p[1]/text()")[2]
是一个基于 xpath-2.0 的表达式,通常 return 一个 object Text
。几个例子:
//@version
:选择与上下文节点在同一个文档中的所有版本属性节点
../@lang
:选择上下文节点父节点的lang属性
You can find the xpath-2.0 specifications in XML Path Language (XPath) 2.0 (Second Edition)
解决方案
实际上你的代码块将是:
if driver.find_elements(By.XPATH, "//*[@id='topstuff']/div/div/p[1]")[2]:
Xpath
您提供的 selenium 尚不支持。
我相信您正在捕获错误网站的错误。
诱导 WebDriverWait
() 并等待 visibility_of_element_located
() 和跟随 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"
我在理解如何执行此操作时遇到了很多问题。我需要做的很简单,就是在我的自动 google 搜索找不到任何搜索结果时进行标记。我的代码示例:
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)
这是我搜索过的 link No Results
我做错了什么?
这个错误信息...
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]")
与:
driver.find_elements(By.XPATH, "//*[@id='topstuff']/div/div/p[1]/text()")[2]
其他注意事项
You can find the xpath-1.0 specifications in XML Path Language (XPath) Version 1.0
但是 xpath 表达式:
driver.find_elements(By.XPATH, "//*[@id='topstuff']/div/div/p[1]/text()")[2]
是一个基于 xpath-2.0 的表达式,通常 return 一个 object Text
。几个例子:
//@version
:选择与上下文节点在同一个文档中的所有版本属性节点../@lang
:选择上下文节点父节点的lang属性
You can find the xpath-2.0 specifications in XML Path Language (XPath) 2.0 (Second Edition)
解决方案
实际上你的代码块将是:
if driver.find_elements(By.XPATH, "//*[@id='topstuff']/div/div/p[1]")[2]:
Xpath
您提供的 selenium 尚不支持。
我相信您正在捕获错误网站的错误。
诱导 WebDriverWait
() 并等待 visibility_of_element_located
() 和跟随 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"