Other SoftDev Categories

Other Software Engineering Categories

Login to WordPress or other accounts using python selenium package

The best way to install the selenium package is using the following command:

$ pip install selenium

or on macOS with Homebrew

$ brew install geckodriver

If you are using Anaconda just simply activate your virtual environment and then install geckodriver using the following command:

$ conda install -c conda-forge geckodriver

Then you’ll need a driver to interface with the browser you choose. I used Firefox in this example, therefore I downloaded the required geckodriver driver.
Drivers for other browser: selenium-python.readthedocs.io
You might need to set the PATH of your geckodriver.

This python source code will pull up a Firefox window and open the url that you need set https://yourwpdomain.com/wp-login.php, then find user_login and user_pass elements on the page (by the id) and finally send a click() function to the submitButton.
If everything goes well the next thing you should see is your WordPress dashboard page in the browser.

from selenium import webdriver

browser = webdriver.Firefox()
#browser = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')  
url = "https://yourwpdomain.com/wp-login.php" # set your WordPress domain

username = browser.find_element_by_id("user_login") 
password = browser.find_element_by_id("user_pass") 

username.send_keys("yourUserName") # add your WordPress UserName
password.send_keys("yourPassword") # add your WordPress Password

submitButton = browser.find_element_by_id("wp-submit") 

If you are using Windows you might need to set the geckodriver PATH with FirefoxDriver.

browser = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')