I need to auto click on any of the "Add" buttons in a web page like as the following address:
"//groceries.asda.com/search/yoghurt"
But, none of the "Add" buttons in the page has name or id. So I can not
use driver.find_element_by_id[]
command from Selenium package.
Can any one help me?
Guy
42.6k10 gold badges36 silver badges78 bronze badges
asked Dec 12, 2019 at 20:04
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
answered Dec 12, 2019 at 21:15
6
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[]
answered Dec 12, 2019 at 20:10
ArnArn
1,81011 silver badges25 bronze badges
2
are you saying you want to click on an add with python? for do that, you can do this:
enter code here
import pyautogui
x= #x location
y= #y location
while True:
pyautogui.click[x,y]
answered Dec 12, 2019 at 20:11
1