Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message: invalid selector: The result of the xpath expression is: [object Text]. It should be an element

I'm trying to print 2 text files under an element separately, but because I can't select text files directly with selenium (like 1st text, 2nd text)

text elements

I want to select 'Home Win' separately 'PSV vs Ajax'

my code:

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")

If I make my code like this, it gives an error;

The result of the xpath expression is: [object Text]. It should be an element.

if I delete '/text()' from the 'matches' element, it takes 2 texts at once.

like image 895
Emre Oz Avatar asked Oct 15 '25 16:10

Emre Oz


1 Answers

This locator

//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']

will give you both the texts while this one

//div[@class='predictionsList']//div[@class='betWrapper ']//div[@class='betHeaderTitle']/span[@class='market']

Will give you 'Home Win' only.
So, you can get the total string and remove the first part string from it to get the second part.
So, to get the second string only, actually the parent element text without child element text you can do something like this:

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, '')
like image 77
Prophet Avatar answered Oct 17 '25 06:10

Prophet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!