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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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 browser.get(url) 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") submitButton.click() |
If you are using Windows you might need to set the geckodriver PATH with FirefoxDriver.
1 |
browser = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe') |