Rajesh
Rajesh

Reputation: 31

Python Selenium: How to get text from span class

I want to get text from span class using selenium webdriver below is the code which I have tried but no result

Output should: Marketplace listings – 72

Code trials:

driver.find_element(By.XPATH,"//h2[@class='jxuftiz4 jwegzro5 hl4rid49 icdlwmnq gvxzyvdx aeinzg81']//span").get_attribute("innerText")

Here is the html

<div class="bdao358l om3e55n1 g4tp4svg alzwoclg cqf1kptm jez8cy9q gvxzyvdx i0rxk2l3 laatuukc gjezrb0y abh4ulrg">
    <div class="om3e55n1 g4tp4svg bdao358l alzwoclg cqf1kptm jez8cy9q gvxzyvdx">
        <div class="bdao358l om3e55n1 g4tp4svg alzwoclg cqf1kptm cgu29s5g dnr7xe2t">
            <div class="bdao358l om3e55n1 g4tp4svg alzwoclg cqf1kptm jez8cy9q gvxzyvdx r227ecj6 gt60zsk1">
                <div class="bdao358l om3e55n1 g4tp4svg tccefgj0 ebnioo9u m733yx0p"></div>
            </div>
            <div class="bdao358l om3e55n1 g4tp4svg alzwoclg cqf1kptm jez8cy9q gvxzyvdx r227ecj6 gt60zsk1">
                <div class="om3e55n1 g4tp4svg bdao358l alzwoclg cqf1kptm jez8cy9q gvxzyvdx o9wcebwi h6ft4zvz">
                    <div class="bdao358l om3e55n1 g4tp4svg alzwoclg cqf1kptm cgu29s5g dnr7xe2t">
                        <div class="bdao358l om3e55n1 g4tp4svg alzwoclg cqf1kptm jez8cy9q gvxzyvdx">
                            <div class="alzwoclg cqf1kptm siwo0mpr gu5uzgus">
                                <div class="jroqu855 nthtkgg5">
                                    <h2 class="jxuftiz4 jwegzro5 hl4rid49 icdlwmnq gvxzyvdx aeinzg81" dir="auto">
                                        <span class="gvxzyvdx aeinzg81 t7p7dqev gh25dzvf exr7barw b6ax4al1 gem102v4 ncib64c9 mrvwc6qr sx8pxkcf f597kf1v cpcgwwas m2nijcs8 hxfwr5lz hpj0pwwo sggt6rq5 innypi6y pbevjfx6 ztn2w49o" dir="auto">
                                            <span class="b6ax4al1 lq84ybu9 hf30pyar om3e55n1 tr46kb4q" style="-webkit-box-orient: vertical; -webkit-line-clamp: 2; display: -webkit-box;">Marketplace listings – 72</span>
                                        </span>
                                    </h2>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Upvotes: 1

Views: 2218

Answers (2)

Alexander
Alexander

Reputation: 17291

Try using this xpath expression instead:

driver.find_element(By.XPATH,"//span[@dir='auto']/span").get_attribute("innerText")

I tested the expression with your HTML example and confirmed it works.

OUTPUT

'Marketplace listings – 72'

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193088

Considering the effective portion from the given HTML:

<h2 class="jxuftiz4 jwegzro5 hl4rid49 icdlwmnq gvxzyvdx aeinzg81" dir="auto">
    <span class="gvxzyvdx aeinzg81 t7p7dqev gh25dzvf exr7barw b6ax4al1 gem102v4 ncib64c9 mrvwc6qr sx8pxkcf f597kf1v cpcgwwas m2nijcs8 hxfwr5lz hpj0pwwo sggt6rq5 innypi6y pbevjfx6 ztn2w49o" dir="auto">
        <span class="b6ax4al1 lq84ybu9 hf30pyar om3e55n1 tr46kb4q" style="-webkit-box-orient: vertical; -webkit-line-clamp: 2; display: -webkit-box;">Marketplace listings – 72</span>
    </span>
</h2>

To extract the text Marketplace listings – 72 ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h2[dir="auto"] span[dir="auto"] > span[style*='webkit-box-orient']"))).text)
    
  • Using XPATH and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[@dir="auto"]/span[@dir="auto"]/span[contains(@style, 'webkit-box-orient') and text()]"))).get_attribute("innerHTML"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python

Upvotes: 0

Related Questions