0

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"}

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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:

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.