Home > Software design >  Selenium XPATH: Iterate through child elements with different class names
Selenium XPATH: Iterate through child elements with different class names

Time:08-24

I want to iterate through child elements of parent <div >. Access the child element, extract some information and move on to next child element.

enter image description here

Unable to loop all the elements since the child class name changes. Suggest an alternative to achieve this

pack_sizes_elements =  wd.find_element(By.XPATH , "//div[@class = '_2Z6Vt   _3vDTQ rippleEffect']")
for element in pack_sizes_elements:
     #MRP 
     mrp =  element.find_element(By.XPATH, "//td[@data-qa ='productPrice']").text.strip()
     

CodePudding user response:

The xpath expression //div[@class='_1LiCn']/div will create the desired iterable list of elements

pack_sizes_elements =  wd.find_elements(By.XPATH , "//div[@class='_1LiCn']/div")
for element in pack_sizes_elements:
     #MRP 
     mrp =  element.find_element(By.XPATH, ".//td[@data-qa ='productPrice']").text.strip()
     
  • Related