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)
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.
Solution 1:
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, '')