Python is really good for automation and data analysis tasks. In this article, I will show you how to login to any WordPress site using a simple python script along with the Selenium module.
How to login to WordPress using Python script
At first import these modules
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
First of all need to install Selenium module using the following command.
pip install selenium
#for linux / mac
pip3 install selenium
Now we are going to write a function which will login to any WordPress site. We will send 3 parameter here which is WebsiteURL, username and password.
def wp_login( website , username , password ):
driver = webdriver.Chrome('D:\\chromedriver.exe')
driver.get( website )
user_field = driver.find_element_by_id('user_login')
user_field.send_keys( username )
pass_field = driver.find_element_by_id('user_pass')
pass_field.send_keys( password )
submit_button = driver.find_element_by_id('wp-submit')
submit_button.submit()
time.sleep( 10 )
driver.quit()
That’s it. So now you have a function which can be used to login any WordPress site from this python script. Lets call this function
wp_login( 'https://pythonmama.com/wp-login.php' , 'username', "stupid_password")
Related articles: