im trying to delete all product from cart. So i search for a loop who : if element exist i click on this element else this element doest more exist. So if not exist i can go to the next step. I write this code but not working :
WebDriverWait(self.driver, 30).until(
EC.presence_of_all_elements_located((By.XPATH, '//*[@data-testid="RemoveProductBtn_btn"]')))
if len(self.driver.find_elements_by_xpath('//*[@data-testid="RemoveProductBtn_btn"]')) > 0:
self.driver.find_element_by_xpath('//*[@data-testid="RemoveProductBtn_btn"]').click()
CodePudding user response:
Why don't you try to handle the exception instead?
try:
WebDriverWait(self.driver, 30).until(
EC.presence_of_all_elements_located((By.XPATH, '//*[@data-testid="RemoveProductBtn_btn"]')))
except ElementNotFoundException:
self.driver.find_element_by_xpath('//*[@data-testid="RemoveProductBtn_btn"]').click()
CodePudding user response:
Use try..except block. it will check if elements present then iterate and click else go except block and then continue.
while True:
try:
elements=WebDriverWait(self.driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@data-testid="RemoveProductBtn_btn"]')))
for element in elements:
element.click()
except:
print("no more products available")
break
#next step
I think you will be always able to click one element and then page will refreshed. so use this code block.
while True:
try:
elements=WebDriverWait(self.driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@data-testid="RemoveProductBtn_btn"]')))
elements[0].click()
except:
print("no more products available")
break