How to login on Facebook with Python and Selenium





Introduction



There are many tutorials that teach how to log in to Facebook with Python and Selenium. Based on my experience, Facebook updates its code often. This fact makes some of the tutorials outdated.

After some research on the topic, I came to the conclusion that almost all the tutorials failed to properly locate the login button with the help of Selenium. 

So I decided to play with python code and finally managed to properly locate the login button. 



Create the initial script


The first step in coding is to create an initial script. And work on it. It improves based on the trial and error process.

Let's import the required utilities and create a driver object.


1
2
3
4
from selenium import webdriver


driver = webdriver.Chrome()


The next step is to define global variables for storing our credentials.

1
2
3
4
5
6
7
8
from selenium import webdriver


driver = webdriver.Chrome()


username_plain = "youremail@some.com"
password_plain = "yoursecret"


Locate the elements of the login form



The fields that should be located are listed below.

  • email
  • password


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from selenium import webdriver


driver = webdriver.Chrome()


username_plain = "youremail@some.com"
password_plain = "yoursecret"


username = driver.find_element_by_id('email')
password = driver.find_element_by_id('pass')

Selenium offers a method that automatically fills the form.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from selenium import webdriver


driver = webdriver.Chrome()


username_plain = "youremail@some.com"
password_plain = "yoursecret"


username = driver.find_element_by_id('email')
password = driver.find_element_by_id('pass')

username.send_keys(username_plain)
password.send_keys(password_plain)


Once the form is filled with the correct credentials, the only thing that is left is to click on the login button.



Locate the login button with the help of the code shown below.

login_button = driver.find_element_by_xpath("//button[text()='Log In']")


Once the button has been located successfully, it's possible to use its click() attribute.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from selenium import webdriver


driver = webdriver.Chrome()


username_plain = "youremail@some.com"
password_plain = "yoursecret"


username = driver.find_element_by_id('email')
password = driver.find_element_by_id('pass')

username.send_keys(username_plain)
password.send_keys(password_plain)


login_button = driver.find_element_by_xpath("//button[text()='Log In']")

login_button.click()

The script is not ready yet


Before logging in on Facebook, it is required to get the login page. The driver instance helps to do it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from selenium import webdriver


driver = webdriver.Chrome()


username_plain = "youremail@some.com"
password_plain = "yoursecret"


driver.get("http://www.facebook.com/")

username = driver.find_element_by_id('email')
password = driver.find_element_by_id('pass')


username.send_keys(username_plain)
password.send_keys(password_plain)


login_button = driver.find_element_by_xpath("//button[text()='Log In']")

login_button.click()


Final thoughts


There are many similar practices shared online. They fail to properly locate the login button on Facebook. I have seen many computer nerds seeking a solution on forums. So I thought to share my experience with the main purpose of helping others. 

If you're having trouble with the script, feel free to comment here, and I will reply back.

© 2022 Copyright by orthodoxpirate.blogspot.com
All Rights Reserved

No comments

Powered by Blogger.