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'
.
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.
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.
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.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With