On Selenium Webdriver How to Get Text from Span Tag

Get text from span tag with Python and Selenium

To print the text Director you can use either of the following Locator Strategies:

  • Using css_selector and get_attribute("innerHTML"):

    print(driver.find_element(By.CSS_SELECTOR, "h2.jobTitle > span[title]").get_attribute("innerHTML"))
  • Using xpath and text attribute:

    print(driver.find_element(By.XPATH, "//h2[contains(@class, 'jobTitle')]/span[@title]").text)

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.jobTitle > span[title]"))).text)
  • Using XPATH and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[contains(@class, 'jobTitle')]/span[@title]"))).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



References

Link to useful documentation:

  • get_attribute() method Gets the given attribute or property of the element.
  • text attribute returns The text of the element.
  • Difference between text and innerHTML using Selenium

Selenium WebDriver -- get <span> text

As the element is within a <span> tag, to extract the text $726.35 you can use either of the following solution:

  • cssSelector:

    driver.findElement(By.cssSelector("span.calculateur-resultats-total#paiement-resultats")).getText();
  • xpath:

    driver.findElement(By.xpath("//span[@class='calculateur-resultats-total' and @id='paiement-resultats']")).getText();

Update

As the results were unstable you can induce WebDriverWait for the visibilityOfElementLocated and you can use either of the following solutions:

  • cssSelector:

    String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table[aria-label='User']"))).getAttribute("innerHTML");
  • xpath:

    String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[contains(@style,'vertbar')]"))).getAttribute("innerHTML");

Extract text from span element using selenium in python

To get the Mobile-Number you can take the reference of the text Mobile and find the next sibling.

Induce WebDriverWait() and wait for visibility_of_element_located() and following xpath.

print(WebDriverWait(browser,10).until(EC.visibility_of_element_located((By.XPATH,"//td[.//div[contains(.,'Mobile')]]/following-sibling::td[1]/div"))).text)

You need to import following libraries.

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

Selenium Python extract text between Span

You need to call the .text method which is present in the Selenium Python binding.

.text is present for web element

authors = driver.find_element_by_xpath("//div[@class='author-info hidden-md']/span/a/span").text
print(authors)

or

authors = driver.find_element_by_xpath("//a[contains(@href,'/author/Margareta-Osborn')]").get_attribute('innerHTML')
print(authors)

Update 1 :

driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://www.bookdepository.com/Rose-River-Margareta-Osborn/9781925324402")
authors = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.author-info.hidden-md span[itemprop='author'] span"))).text
print(authors)

How to get text which is inside the span tag using selenium webdriver?

Problem:

As far as I understand, departmentCategoryContent is a list, not a single WebElement, then it doesn't have the find_elements_by_tag_name() method.

Solution:

you can choose 1 of 2 ways below:

  1. You need for-each of list departmentCategoryContent first, then find_elements_by_tag_name().

  2. Save time with one single statement, using find_elements_by_css_selector():

departmentCategory = driver.find_elements_by_css_selector('.a-spacing-micro.apb-browse-refinements-indent-2 .a-list-item span')

[ print(x.text) for x in departmentCategory ]

Test on devtool:

Sample Image


Explanation:

Your locator .a-list-item span will return all the span tag belong to the div that has class .a-list-time. There are 88 items containing the unwanted tags.

Sample Image

So, you need to add more specific locator to separate the other div. In this case, I use some more classes. .a-spacing-micro.apb-browse-refinements-indent-2

Sample Image



Related Topics



Leave a reply



Submit