Reputation: 1055
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
Reputation: 140
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
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
Link to useful documentation:
get_attribute()
method Gets the given attribute or property of the element.
text
attribute returns The text of the element.
Upvotes: 1
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