Selenium XPATH: Iterate through child elements with different class names
Question:
I want to iterate through child elements of parent <div class="_1LiCn">
. Access the child element, extract some information and move on to next child element.
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()
Answers:
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()
I want to iterate through child elements of parent <div class="_1LiCn">
. Access the child element, extract some information and move on to next child element.
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()
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()