How to extract a text in double quotes inside div with XPath

Viewed 327

(Sorry - I frequent just Stack Overflow em Português but got no help this time, and my English isn't so good. But let me try:)

I want to extract a text that is variable conform the time of the soccer game basically, but I can't find this with XPath. And frankly, the HTML of the site doesn't help so much.

The HTML is this:

<div class="style_title__3UbkX"> 
    "name_team_home"
    "0"
    <span>x</span>
    "2"
    "name_team_away"
</div>

If the teams are Barcelona and Real Madrid, it appears like this in HTML code:

<div class="style_title__3UbkX">
    "Real Madrid"
    "0"
    <span>x</span>
    "2"
    "Barcelona"
</div>

And in the site it will appear in the center of the site something like

Real Madrid 0 x 2 Barcelona

(I can't send images yet)

But it can be different in different minutes of the game, but it doesn't matter, so if the Real Madrid team score a goal, the number in quote will change to 1.

This HTML for me is so strange because it's not separated. Is like this, it's a real copy from the site. I'm not used to use HTML, so I don't know if it's is normal tbh.

I want to extract the numbers in quote, 0 and 2 for a variable, but my code doesn't find this information. I'm trying to use XPath methods, like in this code, but both can't find the information:

goals_h = driver.find_element_by_xpath(
    '//*[@id="root"]/div/section/section/main/div/div/div[2]/div/div/div[2]/text()[2]').text

or

goals_h = wait.until(EC.visibility_of_element_located((By.XPATH,  
    '//*[@id="root"]/div/section/section/main/div/div/div[2]/div/div/div[2]/text()[2]'))).text

I want the value in text()[2] in this XPath.

Someone know how to resolve this?

Thanks in advance.

2 Answers

For a good solution, I would do what James Oltmans suggests above.

For a BAD solution:

normalize-space(substring-after(substring-after(//div[@class="style_title__3UbkX"]/text()[1], "\n"),"\n"))

and

normalize-space(substring-before(substring-after(//div[@class="style_title__3UbkX"]/text()[2], "\n"), "\n"))

You would still have to remove the quotation marks with string processing, but this is, after all, a bad solution.

This XPath will return 2:
"substring-after(substring-before(normalize-space(//*[@id='root']/descendant::div[span[.='x']]/text()[2]),'\" \"'),'\"')"

General form:

substring-after(
  substring-before(
    normalize-space(haystack),
  needle-before),
needle-after)

Tested on this html fragment

<div id="root">
<section>
    <div>
            <div class="style_title__3UbkX">
                "name_team_home"
                "0"
                <span>x</span>
                "2"
                "name_team_away"
            </div>
    </div>
</section>
</div>

With lxml:

>>> from lxml import html
>>> tree = html.parse(r'/home/lmc/tmp/test.html')
>>> result = tree.xpath("substring-after(substring-before(normalize-space(//*[@id='root']/descendant::div[span[.='x']]/text()[2]),'\" \"'),'\"')")
>>> result
'2'

The other XPath is:
"substring-before(substring-after(normalize-space(//*[@id='root']/descendant::div[span[.='x']]/text()[1]),'\" \"'),'\"')"

Result:
0
General form:

substring-before(
  substring-after(
    normalize-space(haystack),
  needle-after),
needle-before)

The key is this XPath
"normalize-space(//*[@id='root']/descendant::div[span[.='x']]/text()[2])"
that returns
"2" "name_team_away"
then the substring to 'cut' the string is
" "

Related