How do you automate a button click in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Selenium is a tool that provides APIs to automate a web application to aid in its testing. In this article, we discuss the use of Selenium Python API bindings to access the Selenium WebDrivers to click a button by text present in the button. In the following example, we take the help of Chrome. The method used is the find_element_by_link_text() which scrapes the element using the text present. In case there is no such element with the given text attribute, NoSuchElementException is returned.

    Installation:

    Make sure you have Selenium installed using

    pip3 install Selenium

    And also download the WebDriver for your web browser :

    Chrome : https://chromedriver.chromium.org/downloads
    Firefox : https://github.com/mozilla/geckodriver/releases
    Safari : https://webkit.org/blog/6900/webdriver-support-in-safari-10/

    Once Selenium is installed along with the desired WebDriver, we create a file script.py and using our code editor write the python script below which opens up the geeksforgeeks website using the Selenium WebDriver and clicks the Sign In button using the link text.

    Syntax:

    driver.find_element_by_link_text("sample text")

    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.

    Below is the implementation.

    Python3

    from selenium import webdriver

    import time

    driver = webdriver.Chrome(r"./driver/chromedriver")

    driver.maximize_window()

    time.sleep(10)

    button = driver.find_element_by_link_text("Sign In")

    button.click()

    Output:

    https://media.geeksforgeeks.org/wp-content/uploads/20210222231422/output_FTOFsx0Z_Tx7e.mp4

    First, the WebDriver opens up the window with geeksforgeeks, maximizes it, and waits for 10 seconds. Then it clicks the Sign In button and opens up the sign-up panel.

    I have a script that is used to login in to a company-made application and click the right buttons like "continue", or "ok", etc. to perform a certain process. However, I have had to use screenshots of these buttons to click in order to do this using pyautogui. Is there any package or way to automate this process without using images. Maybe it can detect the text of the button and click it. I do not have identifiers for the buttons available and no access to the code/info behind the application. Let me know if you have any ideas. Thanks!

    asked Jun 22 at 17:50

    1 Answer

    I have a few questions that may be helpful:

    1. Does the layout of the buttons change? If it's always the same you can just program the correct locations and timing and not worry about reading the screen.
      If you really have to read the screen, look into optical character recognition (ocr).
    2. Is the application keyboard accessible? If so, using Tab and Enter to activate the buttons is simpler than controlling the mouse. Also, if it was made by superstars you can use find (ctrl-F) to search for the text on the buttons and go to them.

    This answer is pretty vague, but I can only be as specific as the question asked.

    answered Jun 22 at 19:41

    Jelly JoeJelly Joe

    381 silver badge7 bronze badges

    1

    • No that was a great response. So unfortunately there are no keys you can use and ctrl-f would not work in the program either. Really great lol. I will look into the OCR however, that was a good tip.

      Jun 23 at 12:29

    Not the answer you're looking for? Browse other questions tagged python button automation click build-automation or ask your own question.

    How do you automatically click a button in Python?

    Steps by step Approach: 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.

    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 automate a program using Python?

    In this article.
    Set up your development environment..
    Install Python..
    Install Visual Studio Code..
    Install the Microsoft Python extension..
    Open the integrated PowerShell terminal in VS Code..
    Install Git (optional).
    Example script to display the structure of your file system directory..

    How do you click in Python?

    First, let's see how we can simulate mouse clicks:.
    import mouse # left click mouse. ... .
    In [22]: mouse. ... .
    # drag from (0, 0) to (100, 100) relatively with a duration of 0.1s mouse. ... .
    # whether the right button is clicked In [25]: mouse. ... .
    # move 100 right & 100 down mouse. ... .
    # make a listener when left button is clicked mouse..