Hướng dẫn python webbrowser click button

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:

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged python html web-scraping or ask your own question.
  • Installation:
  • How do you click a button on a website using Python?
  • How do I automatically click a button on a Web page?
  • Can you use Python on a webpage?
  • How do I run a Python Button in html?

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

Hướng dẫn python webbrowser click button

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 a button on a website using 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..

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

    How to Automate Clicks Using JavaScript.

    .

    document. getElementById("clickMe"). click();.

    for ( let i = 0; i < 1000; i++ ) { document.getElementById("clickMe").click(); }.

    Can you use Python on a webpage?

    The Python programming language can be used to create a huge variety of different types of things, including websites. Making websites with Python is easier than most people think because of the fact that this language makes use of something called “frameworks.”

    How do I run a Python Button in html?

    Invoking a python script on click of html button can be accomplished using python-django framework..

    from flask import Flask..

    app = Flask(__name__).

    @app. route("/").

    def hello():.

    #do your things here..

    return "It works!".

    if __name__ == "__main__":.

    app. run().