How to click a button automatically in python

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 has name or id. So I can not use driver.find_element_by_id() command from Selenium package.

Can any one help me?

Guy

43.2k10 gold badges36 silver badges79 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

How to click a button automatically in python

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,82012 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

How to click button selenium python?

self.driver.find_element_by_xpath("//button[@id='sample_editable_1_new']").click()
Got Error : raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@id='sample_editable_1_new']"}
  (Session info: chrome=86.0.4240.75)

How to click a button automatically in python
Oct 14, 2020 in Python by
• 140 points
15,436 views

2 answers to this question.

Hey, @Parthiv,

For python, use the

from selenium.webdriver import ActionChains

and

ActionChains(browser).click(element).perform()

How to click a button automatically in python
answered Oct 14, 2020 by Gitika
• 65,890 points

Selenium can automatically click on buttons that appear on a webpage

  1. Find the button.
  2. Click on the button.

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.

Syntax :

# finding the button using ID
button = driver.find_element_by_id(ID)

# clicking on the button
button.click()

CODE:

import time

# importing webdriver from selenium

from selenium import webdriver

  

# Here Chrome  will be used

driver = webdriver.Chrome()

# URL of website

url = "https:// ABC.."

  

  # Opening the website

driver.get(url)

  

# geeting the button by class name

button = driver.find_element_by_class_name("slide-out-btn")

  

# clicking on the button

button.click()

This will click on the button and a popup will be shown. For further understanding, you can refer to the Selenium Training.

Hope this helps!!!

How to click a button automatically in python
answered Oct 14, 2020 by Danish Bhatiya

  • All categories
  • How to click a button automatically in python
    Apache Kafka (84)
  • How to click a button automatically in python
    Apache Spark (596)
  • How to click a button automatically in python
    Azure (131)
  • How to click a button automatically in python
    Big Data Hadoop (1,907)
  • How to click a button automatically in python
    Blockchain (1,673)
  • How to click a button automatically in python
    C# (124)
  • How to click a button automatically in python
    C++ (268)
  • How to click a button automatically in python
    Career Counselling (1,060)
  • How to click a button automatically in python
    Cloud Computing (3,356)
  • How to click a button automatically in python
    Cyber Security & Ethical Hacking (145)
  • How to click a button automatically in python
    Data Analytics (1,266)
  • How to click a button automatically in python
    Database (853)
  • How to click a button automatically in python
    Data Science (75)
  • How to click a button automatically in python
    DevOps & Agile (3,500)
  • How to click a button automatically in python
    Digital Marketing (111)
  • How to click a button automatically in python
    Events & Trending Topics (28)
  • How to click a button automatically in python
    IoT (Internet of Things) (387)
  • How to click a button automatically in python
    Java (1,163)
  • How to click a button automatically in python
    Kotlin (3)
  • How to click a button automatically in python
    Linux Administration (384)
  • How to click a button automatically in python
    Machine Learning (337)
  • How to click a button automatically in python
    MicroStrategy (6)
  • How to click a button automatically in python
    PMP (423)
  • How to click a button automatically in python
    Power BI (516)
  • How to click a button automatically in python
    Python (3,154)
  • How to click a button automatically in python
    RPA (650)
  • How to click a button automatically in python
    SalesForce (92)
  • How to click a button automatically in python
    Selenium (1,569)
  • How to click a button automatically in python
    Software Testing (56)
  • How to click a button automatically in python
    Tableau (608)
  • How to click a button automatically in python
    Talend (73)
  • How to click a button automatically in python
    TypeSript (124)
  • How to click a button automatically in python
    Web Development (2,994)
  • How to click a button automatically in python
    Ask us Anything! (66)
  • How to click a button automatically in python
    Others (1,024)
  • How to click a button automatically in python
    Mobile Development (46)

Subscribe to our Newsletter, and get personalized recommendations.

Already have an account? Sign in.

Can you make an auto clicker with Python?

Use the pyautogui Module to Create an Auto Clicker in Python We can use the pyautogui. click() function to click the mouse. We can move the mouse beforehand to the required position using the pyautogui. moveTo() function and specify the coordinates for the cursor.

How do you click a button using Selenium in Python?

You first need to install the latest version of Selenium WebDriver from this link: https://www.selenium.dev/. Make sure you download the web driver version compatible with your browser..
Import dependencies..
Open browser using Selenium..
Search for and click the button..
Close browser..

How do you click an element in Python?

We can click a button with Selenium webdriver in Python using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.

How do you automate a website login in Python?

Stepwise Implementation: First of all import the webdrivers from the selenium library. Find the URL of the login page to which you want to logged in. Provide the location executable chrome driver to selenium webdriver to access the chrome browser.