Reputation: 2515
I am trying to extract data from two divisions in XHTML having the same class name using python but when I try to take out their xpaths, they are different. I tried using
driver = webdriver.Chrome()
content = driver.find_element_by_class_name("abc")
print content.text
but it gives only the content of first div. I heard this can be done using xpath. The xpaths of the divs are as follows:
//*[@id="u_jsonp_2_o"]/div[2]/div[1]/div[3]
//*[@id="tl_unit_-5698935668596454905"]/div/div[2]/div[1]/div[3]
//*[@id="u_jsonp_3_c"]/div[2]/div[1]/div[3]
What I thought, since each xpath has same ending, how can we use this similarity in ending and then access the divisions in python by writing [1],[2],[3].... at the end of the xpath?
Also, I want make content an array containing all the content of classes named abc
. Moreover, I don't know how many abc
s exist! How to integrate the data of all of them in one content array?
Upvotes: 0
Views: 1627
Reputation: 6909
In your case it doesn't matter if you use class name or css, you are only searching for "one" element with find_element
but you want to find several elements:
you need to use find_element**s**_by_class_name
content = driver.find_elements_by_class_name("abc")
for element in content:
// here you can work with every single element that has class "abc"
// do whatever you want
print element.text
Upvotes: 2