How Do You Know if a Button Is Enabled or Disabled on a Web Page Using Selenium
· five min read · Updated sep 2021 · Web Scraping
Controlling a spider web browser from a plan can exist useful in many scenarios, case use cases are website text automation and web scraping, a very pop framework for this kind of automation is Selenium WebDriver.
Selenium WebDriver is a browser-controlling library, it supports all major browsers (Firefox, Edge, Chrome, Safari, Opera, etc.) and is available for different programming languages including Python. In this tutorial, nosotros volition be using its Python bindings to automate login to websites.
Automating the login process to a website proves to be handy. For case, yous may want to edit your account settings automatically, or yous want to extract some information that requires login, etc.
We take a tutorial on extracting web forms using the BeautifulSoup library, so you may want to combine extracting login forms and filling them with the help of this tutorial.
Kickoff, permit's install Selenium for Python:
pip3 install selenium
The next step is installing the driver specific to the browser we desire to control, download links are available on this folio. I'm installing ChromeDriver, only you're gratis to use your favorite.
To brand things physical, I'll be using the Github login page to demonstrate how you can automatically log in using Selenium.
Open up a new Python script and initialize the WebDriver:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait # Github credentials username = "username" password = "password" # initialize the Chrome driver driver = webdriver.Chrome("chromedriver")
After yous downloaded and unzipped the driver for your Os, put it in your current directory or in a known path, so you can laissez passer it to webdriver.Chrome()
grade. In my instance, chromedriver.exe is in the electric current directory, then I simply laissez passer its name to the constructor.
Since we're interested in automating Github login, we'll navigate to Github login folio and inspect the folio to identify its HTML elements:
The
id
of the login and password input fields, and the proper noun of the Sign-in push will be useful for united states of america to call up these elements in lawmaking and insert to it programmatically.
Notice the username/electronic mail address input field has login_field
id
, where the password input field has the id
of password
, encounter also the submit button has the proper noun
of commit
, the below code goes to the Github login page, extracts these elements, fills the credentials, and clicks the push button:
# caput to github login page commuter.get("https://github.com/login") # notice username/email field and send the username itself to the input field commuter.find_element_by_id("login_field").send_keys(username) # find password input field and insert password too commuter.find_element_by_id("password").send_keys(countersign) # click login push driver.find_element_by_name("commit").click()
The find_element_by_id()
office retrieves an HTML element by its id
, and the send_keys()
method simulates keypresses, the to a higher place code cell will make Chrome type in the electronic mail and the password, and then click the Sign in push button.
The next matter to practise is to determine whether our login was successful, there are a lot of means to find that, merely in this tutorial, we'll do it by detecting the shown errors upon login (of grade, this volition change from a website to another).
The above image shows what happens when we insert wrong credentials, you'll come across a new HTML
div
element with the grade "flash-mistake"
that has the text of "Incorrect username or countersign.".
The beneath lawmaking is responsible for waiting for the page to be loaded after the login is performed using WebDriverWait()
, and checks for the fault:
# wait the set up state to be consummate WebDriverWait(driver=driver, timeout=x).until( lambda x: x.execute_script("render document.readyState === 'complete'") ) error_message = "Incorrect username or password." # get the errors (if there are) errors = commuter.find_elements_by_class_name("flash-error") # print the errors optionally # for east in errors: # print(east.text) # if we find that error bulletin inside errors, then login is failed if whatsoever(error_message in e.text for due east in errors): print("[!] Login failed") else: print("[+] Login successful")
We use WebDriverWait to look until the certificate finished loading, the execute_script()
method executes Javascript in the context of the browser, the JS code return document.readyState === 'complete'
returns True
when the folio is loaded, and False
otherwise.
Finally, we close our driver:
# close the driver driver.close()
Decision
Alright, now you lot have the skill to log in automatically to the website of your option, notation that Github will cake you lot when you run the script multiple times with wrong credentials, then be aware of that.
Now you can do the thing you want to practice afterwards you log in using your account, you tin can add the lawmaking in the line where we're printing 'Login successful' .
Also, if yous've successfully logged in using your real account, you may run across electronic mail confirmation, to featherbed that, you lot have to read your email programmatically with Python and extracts the confirmation code, and insert it in existent-time using Selenium, great challenge, isn't it? Good luck with it!
Notation that the login process will differ from one website to some other, the goal of this tutorial is to give you the essential skills to automate the login of your target website. If you lot want to accelerate your web scraping skills, the below courses will definitely be valuable for you, check them out:
- Modern Web Scraping with Python using Scrapy Splash Selenium
- Web Scraping and API Fundamentals in Python 2021
Bank check the full code here.
Larn also: How to Use Github API in Python.
Happy Automating ♥
View Full Lawmaking
Read Also
Annotate panel
Source: https://www.thepythoncode.com/article/automate-login-to-websites-using-selenium-in-python
Post a Comment for "How Do You Know if a Button Is Enabled or Disabled on a Web Page Using Selenium"