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:

"//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

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?


                                                    Add New
                                                   
                                               

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]

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[]

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 = "// 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!!!

answered Oct 14, 2020 by Danish Bhatiya

Related Questions In Python

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

Chủ Đề