0

I want to get the values contained in the span class with value "indigo-text descfont" for the following:

<div id="WineDetailContent"> event
 <span class="blue-text codefont">...</span>
 <span class="indigo-text descfont">Alsace</span>
 <br>
 <span class="blue-text codefont">...</span>
 <span class="indigo-text descfont">2014</span>
 <br>
</div>

So that I get Alsace and 2014. I have tried using the following:

details = driver.find_element_by_xpath("//div[starts-with(@id,'WineDetailContent')]")
res = details.find_element_by_xpath("//span[starts-with(@class,'indigo-text descfont')]")
print(res.text)

But it only returns the first needed value, i.e. Alsace. How is it possible to get 2014 as well?

SK_33
  • 77
  • 2
  • 4
  • On XPATH when you are looking for something inside an element you have to use (a dot) ".//xx" , that means it will looking for on the element, not "//xx" on the root – Wonka Jun 05 '23 at 09:45

3 Answers3

0

As per the HTML:

<div id="WineDetailContent">
    ...
    <span class="indigo-text descfont">Alsace</span>
    <br>
    ...
    <span class="indigo-text descfont">2014</span>
    <br>
</div>

Both the desired texts are within the decendants <span class="indigo-text descfont"> of their ancestor <div id="WineDetailContent">


Solution

To print the texts from the <span class="indigo-text descfont"> elements you can use list comprehension and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print([my_elem.text for my_elem in driver.find_elements(By.CSS_SELECTOR, "div#WineDetailContent span.indigo-text.descfont")])
    
  • Using XPATH:

    print([my_elem.text for my_elem in driver.find_elements(By.XPATH, "//div[@id='WineDetailContent']//span[@class='indigo-text descfont']")])
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

find_element_by_xpath return single WebElement. You need to use find_elements_by_xpath to select both elements:

details = driver.find_element_by_xpath("//div[@id ='WineDetailContent']")
res = details.find_elements_by_xpath("./span[@class = 'indigo-text descfont']")
for i in res:
    print(i.text)
Curious koala
  • 309
  • 1
  • 9
0

I assume you are missing an (s) in find element, you need to use find_elements.

In general when we apply find element

element.find_element_by_xxxx(selector)

we will get back the first element that matches our selector.

however in case of more items you can use find elements

element.find_elements_by_xxxx(selector)

the find elements will return a list of all elements that matches our selector.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Marcos
  • 1
  • 2