1
<span class="chr-UserDropdownItem-imageAndText">
    <img class="chr-UserDropdownItem-image" src="/slm/profile/image/264023573852/24.sp?version=1" alt="User Profile Avatar">
    <span class="smb-DropdownItem-text">
        <span>   test user   </span>
    </span>
</span>

By taking look on above screenshot please help me extract the innerText test user uniquely from span tag.

The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
deepakbhavale67
  • 347
  • 5
  • 13

4 Answers4

1

To extract the text test user you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and get_attribute():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.smb-DropdownItem-text>span"))).get_attribute("innerHTML"))
    
  • Using XPATH and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "span[@class='smb-DropdownItem-text']>span"))).text)
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

You can use XPath like this for remove white-space:

driver.find_element_by_xpath('//span/text()[normalize-space()="test user"]')

If you don't have white-space in the span tag, use XPath this way:

driver.find_element_by_xpath('//span[contains(text(), "test user")]')

For Dynamically:

driver.find_element_by_xpath('//span[contains(text(), "' . $HereIsSetYourDynamicallyVaribleName . '")]')
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
0

You can find the element using the xpath:

driver.find_element_by_xpath("//span[@class='smb-DropdownItem-text']//span[contains(text(),'test user')]");
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
0

You can give it try with below xpath.

//img[contains(@src, '/profile/image/')]]/following-sibling::span
Muzzamil
  • 2,823
  • 2
  • 11
  • 23