Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text with Selenium WebDriver in Python

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?

like image 806
user3121891 Avatar asked Jan 08 '14 13:01

user3121891


People also ask

How do you getText of a web element in Python Selenium?

getText() Method in Selenium This method helps retrieve the text, which is basically the innertext of a WebElement. getText() method returns string as a result. It removes the whitespaces if present in the front and back of the string.

How do I use Selenium to text?

Selenium offers a getText() method used to get the text of an element, i.e.; it can be used to read text values of an element from a web page.


2 Answers

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.

like image 163
Arran Avatar answered Sep 22 '22 04:09

Arran


Python

element.text 

Java

element.getText() 

C#

element.Text 

Ruby

element.text 
like image 36
Shubham Jain Avatar answered Sep 23 '22 04:09

Shubham Jain