Auto Click A Button In A Web Page
I need to auto click on any of the 'Add' buttons in a web page like as the following address: 'https://groceries.asda.com/search/yoghurt' But, none of the 'Add' buttons in the page
Solution 1:
To click on any particular Add button for a particular product you can write a method as follows:
def click_me(string):
driver.find_element_by_xpath("//h3/a[@class='co-product__anchor' and contains(@title, '%s')]//following::button[1]" % (string)).click()
Now you can click on any of the Add button passing their title as follows:
click_me("Munch") # Munch Bunch Double Up Strawberry & Vanilla Yogurts
# or
click_me("ASDA") # ASDA Greek Style Fat Free Yogurt
# or
click_me("Petits") # Petits Filous Apricot, Strawberry & Raspberry Yogurt
Solution 2:
Use a similar method find_elements_by_css_selector
:
elements = driver.find_elements_by_css_selector(.asda-button.asda-button--category-primary.asda-button--color-green.asda-button--size-small.co-quantity__add-btn)
as the buttons have identifying classes. Afterwards, you can click each of these buttons:
for e in elements:
e.click()
Post a Comment for "Auto Click A Button In A Web Page"