消息:无效的选择器:xpath 表达式的结果是:[object Text]。应该是一个元素
Message: invalid selector: The result of the xpath expression is: [object Text]. It should be an element
我试图在一个元素下分别打印 2 个文本文件,但是因为我不能 select 直接使用 selenium 打印文本文件(比如第一个文本、第二个文本)
我想select'Home Win'分开'PSV vs Ajax'
我的代码:
driver.get("https://footystats.org/predictions/home-wins")
matches = driver.find_elements_by_xpath("//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']/text()")
predictions = driver.find_elements_by_xpath("//div[@class='predictionsList']//div[@class='betWrapper ']//span[@class='market']")
odds = driver.find_elements_by_xpath("//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderMeta']")
for x in range(0,len(matches)):
print(matches[x].text, predictions[x].text, odds[x].text, sep="\t")
如果我这样写代码,它会报错;
The result of the xpath expression is: [object Text]. It should be an element.
如果我从 'matches' 元素中删除“/text()”,它会同时占用 2 个文本。
这个定位器
//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']
这本
会同时给你两本课文
//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']/span[@class='market']
只会给你'Home Win'。
因此,您可以获得总字符串并从中删除第一部分字符串以获得第二部分。
因此,要仅获取第二个字符串,实际上是没有子元素文本的父元素文本,您可以这样做:
total_txt = driver.find_element_by_xpath("//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']").text
child_txt = driver.find_element_by_xpath("//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']/span[@class='market']").text
the_text_you_are_looking_for = total_txt.replace(child_txt, '')
我试图在一个元素下分别打印 2 个文本文件,但是因为我不能 select 直接使用 selenium 打印文本文件(比如第一个文本、第二个文本)
我想select'Home Win'分开'PSV vs Ajax'
我的代码:
driver.get("https://footystats.org/predictions/home-wins")
matches = driver.find_elements_by_xpath("//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']/text()")
predictions = driver.find_elements_by_xpath("//div[@class='predictionsList']//div[@class='betWrapper ']//span[@class='market']")
odds = driver.find_elements_by_xpath("//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderMeta']")
for x in range(0,len(matches)):
print(matches[x].text, predictions[x].text, odds[x].text, sep="\t")
如果我这样写代码,它会报错;
The result of the xpath expression is: [object Text]. It should be an element.
如果我从 'matches' 元素中删除“/text()”,它会同时占用 2 个文本。
这个定位器
//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']
这本
会同时给你两本课文//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']/span[@class='market']
只会给你'Home Win'。
因此,您可以获得总字符串并从中删除第一部分字符串以获得第二部分。
因此,要仅获取第二个字符串,实际上是没有子元素文本的父元素文本,您可以这样做:
total_txt = driver.find_element_by_xpath("//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']").text
child_txt = driver.find_element_by_xpath("//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']/span[@class='market']").text
the_text_you_are_looking_for = total_txt.replace(child_txt, '')