Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the text from multiple elements with the same class in Selenium for Python?

Tags:

I'm trying to scrape data from a page with JavaScript loaded content. For example, the content I want is in the following format:

<span class="class">text</span> ... <span class="class">more text</span> 

I used the find_element_by_xpath(//span[@class="class"]').text function but it only returned the first instance of the specified class. Basically, I would want a list like [text, more text] etc. I found the find_elements_by_xpath() function, but the .text at the end results in an error exceptions.AttributeError: 'list' object has no attribute 'text'.

like image 971
user3685742 Avatar asked May 29 '14 00:05

user3685742


People also ask

How xpath find multiple elements?

We can create an xpath for their identification and utilize the method find_elements_by_xpath. With this, the elements with the matching value of the given xpath are returned in the form of a list. In case there is no element with the matching value of the xpath, an empty list shall be returned.

How do I getText from the list of Webelements?

We can get the text from a list of all web elements with the same class name in Selenium webdriver. We can use any of the locators like the class name with method By. className, xpath with method By. xpath, or css with method By.

How do you handle multiple elements with the same xpath?

For example if both text fields have //input[@id='something'] then you can edit the first field xpath as (//input[@id='something'])[1] and the second field's xpath as (//input[@id='something'])[2] in object repository.


1 Answers

find_element_by_xpath returns one element, which has text attribute.

find_elements_by_xpath() returns all matching elements, which is a list, so you need to loop through and get text attribute for each of the element.

all_spans = driver.find_elements_by_xpath("//span[@class='class']") for span in all_spans:     print span.text 

Please refer to Selenium Python API docs here for more details about find_elements_by_xpath(xpath).

like image 128
Yi Zeng Avatar answered Sep 28 '22 04:09

Yi Zeng