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 i am doing wrong?
no such element: Unable to locate element: {"method":"xpath","selector":".//h2[@hljs-string">"]//a"}
CodePudding user response:
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[@]//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.
CodePudding user response:
To print the text Director you can use either of the following Locator Strategies:
Using
css_selectorandget_attribute("innerHTML"):print(driver.find_element(By.CSS_SELECTOR, "h2.jobTitle > span[title]").get_attribute("innerHTML"))Using
xpathand 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_SELECTORand text attribute:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h2.jobTitle > span[title]"))).text)Using
XPATHandget_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()methodGets the given attribute or property of the element.textattribute returnsThe text of the element.- Difference between text and innerHTML using Selenium
