Python auto click button on website

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

Not the answer you're looking for? Browse other questions tagged python selenium selenium-webdriver webdriver or ask your own question.

How do you automate a button click in Python?

Steps by step Approach:.
Import required modules..
Create webdriver object..
Assign URL..
Use maximize_window[] method to maximize the browser window. And then wait 10 seconds using sleep[] method..
Use find_element_by_link_text[] method to click button by text..

Can I use Python to click button on webpage?

We can find the button on the web page by using methods like find_element_by_class_name[], find_element_by_name[], find_element_by_id[] etc, then after finding the button/element we can click on it using click[] method. This will click on the button and a popup will be shown.

How do I automatically click a button on a web page?

How to Automate Clicks Using JavaScript.
Click Here.
document. getElementById["clickMe"]. click[];.
for [ let i = 0; i < 1000; i++ ] { document.getElementById["clickMe"].click[]; }.

Chủ Đề