Simon GIS
Simon GIS

Reputation: 1055

Get text from span tag with Python and Selenium

I'm trying to get this element with Python and Selenium.

<h2 class="jobTitle jobTitle-color-purple">
    <span title="Director">Director=</span>
<h2>

This is what I tried:

try:
    title  = job.find_element_by_xpath('.//td[@class="title"]//a').text
except:
    title = job.find_element_by_xpath('.//h2[@class="title"]//a').get_attribute(name="title")

What am I doing wrong?

no such element: Unable to locate element: {"method":"xpath","selector":".//h2[@class="title"]//a"}

Upvotes: 0

Views: 5260

Answers (3)

To get The text From a Page

Des = driver.find_element_by_xpath('//*[@class="class name of required data"]').text

Here only the class name of span tag from where you want to get the text

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193058

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:

Upvotes: 1

Prophet
Prophet

Reputation: 33353

You are using absolutely wrong locator.
The h2 element class attribute value is jobTitle jobTitle-color-purple, not title.
The child element tag name is span, not a.
Please try this:

title  = job.find_element_by_xpath('//h2[@class="jobTitle jobTitle-color-purple"]//span').text

You will possibly need to add a wait / delay to add before that to let the page loaded before you trying to access that element.

Upvotes: 2

Related Questions