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,810 points
15,300 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,440 points
0 votes
text = driver.find_element_by_class_name("current-text").getText();
answered Feb 4 by anonymous

Related Questions In Selenium

0 votes
1 answer
0 votes
2 answers

How to get HTML Source of WebElement in Selenium WebDriver using Python?

You can read innerHTML attribute to get source of ...READ MORE

answered Jun 13, 2018 in Selenium by Samarpit
• 5,150 points
18,933 views
0 votes
1 answer

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,110 points
1,145 views
0 votes
1 answer
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,150 points
1,245 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,150 points
999 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,440 points
8,938 views
0 votes
1 answer