I'm trying to get text using Selenium WebDriver and here is my code. Please note that I don't want to use XPath, because in my case the ID gets changed on every relaunch of the web page.
My code:
text = driver.find_element_by_class_name("current-stage").getText("my text")HTML:
<span class="current-text" id="yui_3_7_0_4_1389185744113_384">my text</span>How can I fix this?
MattDMo
98k20 gold badges235 silver badges228 bronze badges
asked Jan 8, 2014 at 13:00
1
You want just .text.
You can then verify it after you've got it, don't attempt to pass in what you expect it should have.
answered Jan 8, 2014 at 13:02
ArranArran
24.1k6 gold badges65 silver badges77 bronze badges
11
Python
element.textJava
element.getText()C#
element.TextRuby*
element.textanswered Aug 24, 2017 at 13:51
Shubham JainShubham Jain
15.4k12 gold badges74 silver badges116 bronze badges
6
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
References
Link to useful documentation:
- get_attribute() method Gets the given attribute or property of the element.
- text attribute returns The text of the element.
- Difference between text and innerHTML using Selenium
answered Jan 23, 2021 at 19:20
The answer is:
driver.find_element_by_class_name("ctsymbol").text
answered Apr 11, 2015 at 4:34
rearThingrearThing
5843 gold badges8 silver badges25 bronze badges
You can use:
element = driver.find_element_by_class_name("class_name").textThis will return the text within the element and will allow you to verify it after that.
answered Dec 23, 2015 at 12:02
This is the correct answer. It worked!!
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait driver = webdriver.Chrome("E:\\Python\\selenium\\webdriver\\chromedriver.exe") driver.get("//www.tatacliq.com/global-desi-navy-embroidered-kurta/p-mp000000000876745") driver.set_page_load_timeout(45) driver.maximize_window() driver.implicitly_wait(2) driver.get_screenshot_as_file("E:\\Python\\Tatacliq.png") print ("Executed Successfully") driver.find_element_by_xpath("//div[@class='pdp-promo-title pdp-title']").click() SpecialPrice = driver.find_element_by_xpath("//div[@class='pdp-promo-title pdp-title']").text print(SpecialPrice)
answered Jul 19, 2017 at 17:46
I've found this absolutely invaluable when unable to grab something in a custom class or changing id's:
driver.find_element_by_xpath("//*[contains(text(), 'Show Next Date Available')]").click() driver.find_element_by_xpath("//*[contains(text(), 'Show Next Date Available')]").text driver.find_element_by_xpath("//*[contains(text(), 'Available')]").text driver.find_element_by_xpath("//*[contains(text(), 'Avail')]").text
answered Aug 26, 2020 at 3:20
3
A heads up for anyone finding this thread after the Selenium 4 update. The driver.find_element_by_* has been deprecated and using it will give a "deprecationwarning". The replacement method is: driver.find_element(By.X,"name") Please look up Selenium 4 info.
answered Aug 11 at 15:54