Get text using selenium web driver in python

0 votes

I'm trying to get text using selenium web driver, I don't want to use XPath because the Id changes every time I launch the web page.

Below is my code:

text=driver.find_element_by_class_name("stage").getText("text")

HTML code:

span class="current-text" id="yui_3_7_0_4_1389185744113_384">text</span
Jun 21, 2018 in Selenium by Christine
• 15,790 points
37,609 views
but how to get text from driver.get_elements_by_class_name ?

Hey @Zash121,

The code in the above question seems wrong IMO. Don't follow that. The correct syntax is:

text = driver.find_element_by_class_name("---abc---").getText();

Here, the classname of the element you're talking about should come in place of: ---abc---. And whatever the visible text is in that element that will be fetched by getText() function and it will be stored in variable text. Just go ahead and use a print statement to display whatever is stored in that variable.

Hey @Zash121, 

For python you have to postfix

text=driver.find_element_by_class_name("stage")

with .text 

and for JAVA with .getText()

Yup. I think that sounds right. I'm not a punter in Python myself, but going by the syntax and logic, it could work. Do let us know otherwise.
Hey @zash121, you can use the following:

driver.findElement(By.xpath("//*[contains(text(),'Your text')]"));

2 answers to this question.

0 votes

You can use the below code:

element = driver.find_element_by_class_name("class_name").text
answered Jun 21, 2018 by Meci Matt
• 9,460 points
0 votes
text = driver.find_element_by_class_name("current-text").getText();
answered Feb 4, 2019 by anonymous

Related Questions In Selenium

0 votes
1 answer
0 votes
2 answers
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,579 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,559 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,606 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,507 views
0 votes
1 answer

How do I get current URL in Selenium Webdriver using Python?

Use current_url element. Example: print browser.current_url READ MORE

answered Aug 8, 2018 in Selenium by Meci Matt
• 9,460 points
25,606 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP