Skip to content

Commit 02916f7

Browse files
committed
add automate login using selenium tutorial
1 parent 14a61d6 commit 02916f7

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
9797
- [How to Extract All PDF Links in Python](https://www.thepythoncode.com/article/extract-pdf-links-with-python). ([code](web-scraping/pdf-url-extractor))
9898
- [How to Extract Images from PDF in Python](https://www.thepythoncode.com/article/extract-pdf-images-in-python). ([code](web-scraping/pdf-image-extractor))
9999
- [Automated Browser Testing with Edge and Selenium in Python](https://www.thepythoncode.com/article/automated-browser-testing-with-edge-and-selenium-in-python). ([code](web-scraping/selenium-edge-browser))
100+
- [How to Automate Login using Selenium in Python](https://www.thepythoncode.com/article/automate-login-to-websites-using-selenium-in-python). ([code](web-scraping/automate-login))
100101

101102
- ### [Python Standard Library](https://www.thepythoncode.com/topic/python-standard-library)
102103
- [How to Transfer Files in the Network using Sockets in Python](https://www.thepythoncode.com/article/send-receive-files-using-sockets-python). ([code](general/transfer-files/))

web-scraping/automate-login/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# [How to Automate Login using Selenium in Python](https://www.thepythoncode.com/article/automate-login-to-websites-using-selenium-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- Get [ChromeDriver](https://sites.google.com/a/chromium.org/chromedriver/home) and put it in the current directory.
5+
- Edit credentials in `automate_login.py` and run it!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from logging import error
2+
from selenium import webdriver
3+
from selenium.webdriver.support.ui import WebDriverWait
4+
5+
# Github credentials
6+
username = "username"
7+
password = "password"
8+
9+
# initialize the Chrome driver
10+
driver = webdriver.Chrome(r"C:\Users\STRIX\Desktop\selenium\chromedriver")
11+
# head to github login page
12+
driver.get("https://github.com/login")
13+
# find username/email field and send the username itself to the input field
14+
driver.find_element_by_id("login_field").send_keys(username)
15+
# find password input field and insert password as well
16+
driver.find_element_by_id("password").send_keys(password)
17+
# click login button
18+
driver.find_element_by_name("commit").click()
19+
# wait the ready state to be complete
20+
WebDriverWait(driver=driver, timeout=10).until(
21+
lambda x: x.execute_script("return document.readyState === 'complete'")
22+
)
23+
error_message = "Incorrect username or password."
24+
# get the errors (if there are)
25+
errors = driver.find_elements_by_class_name("flash-error")
26+
# print the errors optionally
27+
# for e in errors:
28+
# print(e.text)
29+
# if we find that error message within errors, then login is failed
30+
if any(error_message in e.text for e in errors):
31+
print("[!] Login failed")
32+
else:
33+
print("[+] Login successful")
34+
35+
# close the driver
36+
driver.close()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
selenium

0 commit comments

Comments
 (0)