I'm trying to scrap the class name of the first child (span) from multiple div.
Here is the html code:
<div >
<span ...">...</span>
...
<div class ="ui_column is-9">
<span ...">...</span>
...
<div class ..
URL of the page for the complete code.
I'm achieving this task with this code for the first five div:
i=0
liste=[]
while i <= 4:
parent= driver.find_elements_by_xpath("//div[@class='ui_column is-9']")[i]
child= parent.find_element_by_xpath("./child::*")
class_name= child.get_attribute('class')
i = i 1
liste.append(nom_classe)
But do you know if there is an easier way to do it ?
CodePudding user response:
You can directly get all these first span
elements and then extract their class
attribute values as following:
liste = []
first_spans = driver.find_elements_by_xpath("//div[@class='ui_column is-9']//span[1]")
for element in first_spans:
class_name= element.get_attribute('class')
liste.append(class_name)
You can also extract the class
attribute values from 5 first elements only by limiting the loop for 5 iterations
UPD
Well, after updating your question the answer becomes different and much simpler.
You can get the desired elements directly and extract their class name attribute values as following:
liste = []
first_spans = driver.find_elements_by_xpath("//div[@class='ui_column is-9']//span[contains(@class,'ui_bubble_rating')]")
for element in first_spans:
class_name= element.get_attribute('class')
liste.append(class_name)