To print the text my text
you can use either of the following Locator Strategies:
Using class_name
and get_attribute("textContent")
:
print(driver.find_element(By.CLASS_NAME, "current-stage").get_attribute("textContent"))
Using css_selector
and get_attribute("innerHTML")
:
print(driver.find_element(By.CSS_SELECTOR, "span.current-stage").get_attribute("innerHTML"))
Using xpath
and text attribute:
print(driver.find_element(By.XPATH, "//span[@class='current-stage']").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following Locator Strategies:
Using CLASS_NAME
and get_attribute("textContent")
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "current-stage"))).get_attribute("textContent"))
Using CSS_SELECTOR
and text attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.current-stage"))).text)
Using XPATH
and get_attribute("innerHTML")
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='current-stage']"))).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.