How do you click a website in python?

I currently have a script that logs me into a website and I want to have it click a button on the website if it is currently not clicked. Here is the info for the button:

When the button is already active:

When the button is not active:

I am only looking to click it when class="button grey toast track-click"

What is the best way to do this? I currently use urllib2 and mechanize to login and check a few forms currently. Thanks!

asked Jan 9, 2015 at 21:06

1

When I compare the two tags I see that the difference is for the class tag. So if you can read it then you're done

You can do it with Selenium if you like

Step 1: find the XPath - Get the XPath of the button: for that right open the page in Chrome click on it and select Inspect element - It will open the html file and right click on the highlighted line and select copy Xpath - Copy the XPath in NotePad

Now that you have the XPath you can select the button via a Python script and query the attributes

Here is a prototype

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.youradress.org")#put here the adress of your page
elem = driver.find_elements_by_xpath("//*[@type='submit']")#put here the content you have put in Notepad, ie the XPath
button = driver.find_element_by_id('buttonID') //Or find button by ID.
print(elem.get_attribute("class"))
driver.close()

Hope that helps, if you have question please let me know

I used these links for documentation

Python Selenium: Find object attributes using xpath

https://selenium-python.readthedocs.io/locating-elements.html

How do you click a website in python?

Haseeb Mir

8531 gold badge13 silver badges21 bronze badges

answered Jan 9, 2015 at 21:36

GabrielGabriel

3,5041 gold badge25 silver badges48 bronze badges

4

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

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.

    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..

    How do you click on a specific button 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.

    Can Python access a website?

    One useful package for web scraping that you can find in Python's standard library is urllib , which contains tools for working with URLs. In particular, the urllib. request module contains a function called urlopen() that can be used to open a URL within a program.