How to find multiple elements with the same path on the same page using Selenium Xpath
I am trying to pull data "Text I Want" with selenium using python.
The code I used below only pulled out the first one.
e = driver.find_element_by_xpath('.//*[@class = "sth3"]/span')
e.text
What should I do? Page source below:
<li id = "12345" class = "sth">
<div class = "sth1">
<div class = "sth2">
<a>
<div class = "sth3">
<span class = "sth4">Text I Want</span>
<li id = "12345" class = "sth">
<div class = "sth1">
<div class = "sth2">
<a>
<div class = "sth3">
<span class = "sth4">Text I Want</span>
+3
dlfjj
source
to share
2 answers
Try using below code:
text_list = [e.text for e in driver.find_elements_by_xpath('.//*[@class = "sth3"]/span')]
This will allow you to get a list of text content from all the required elements
0
Andersson
source
to share
// First find out the elements having xpath span and store it in the list of elements.
List<WebElements> list_ele = driver.findElements(By.xpath("//span"));
then with the help of for loop print value of each span text.
for(webElement x : list){
system.out.println(x.getText());
}
-1
virender rana
source
to share