Prerequisites:
- Comfortable installing software
- Knowledge extracting files from zipped folders
- Familiar using and navigating the command line (CMD or Powershell)
- Some coding experience with Python is a plus but not needed.
What I Cover:
- Installing and setting up Selenium Webdriver and Python 3
- Setting up the Chrome driver and adding it to the environment path
- Very basic script of opening the Chrome browser and navigating to a web page.
In this post I will be demonstrating how to set up and use Selenium Webdriver with python 3 on Windows 10. For those of you who are not familiar with either, Selenium Webdriver allows us to interact with web browsers to automate the manual process of web interactions. Python is a very powerful and lightweight computer programming language that can be used for creating many things but today were going to use it to write code to interact with the Selenium library.
Let's just dive right in and get the necessary tools if you will to make a simple Python script with Selenium. We will need at least three things to get started.
Installing Python:
Ill paste the direct URL but you can also just Google python download for windows or something similar. https://www.python.org/downloads/windows/. You can use a different version if you want but during the time of this post I will be using 3.7.0.
Below is a screen shot of the one that I selected as it will guide you through the install process. You can keep all of the default options. The main thing you need to pay attention to during the install is checking the box to add python to your environment path! Make sure to check this box during the install. This allows Python to be executed from anywhere on your machine. FYI the download is for both 32bit and 64bit Windows OS. When you see "x86" it means it is for 32bit OS as well.
After the install lets confirm that we have Python set up and it is set in the environment path for our system. Open a command prompt of your choice (e.x...CMD, Powershell) and type "python --version" with out the quotes and hit enter. You should see something like this below.
If you see the version printed on the screen, congratulations you have officially installed Python on your machine! For the next part we need to install selenium with the Python package installer called "PIP". This allows us to use Selenium with Python.
To view the currently installed packages you can type "pip list" with out the quotes and press enter. You may only have a couple of items in there to start. Next lets install selenium with pip by typing "pip install selenium" with out the quotes and press enter. You should see an install come up. Once completed check the list again for selenium.
If you see selenium, great! We can continue on to setting up a selenium webdriver!!!
Setup the chrome driver for Selenium Webdriver:
For this next part we will need to go to the selenium website. Here is the direct link for the downloads page https://www.seleniumhq.org/download/ but again if you want you can easily Google it.
Here we have to decide which browser we want to automate. With my current experience I believe that Chrome is the simplest to use right out of the box. What I mean by that is for example if we were to use the IE driver, the driver will not work correctly unless our IE settings are set a specific way where as with chrome there are no pre-set settings.
I will make a separate post for IE if there are any request for it.
On the download page scroll down until you see the following links and click on the select link for the Google Chrome Driver.
At the time of this post the latest Chrome driver is 2.42 but yours may differ, that's okay just select the link to be taken to a list of all the available downloads.
You may notice that there is actually no download for Windows 64bit with the Chrome driver, that is okay we will want to use the 32bit driver. Also notice that this is a zip file. We just want to get the executable file from the zipped folder and place it somewhere on your machine. You can place this anywhere but if you want to ahead of time create a new folder on your C:\ drive called selenium and inside of that folder create another one called chrome and inside of there create a new folder with the version and extract the file into that folder.
Once you have it downloaded simply right click on the zipped folder and extract to the same location. If you created the suggested folder path it will look something like the image below. If you want to place the file somewhere else that is okay too.
For the next step we will need to add the executable chrome driver to our environment path. First copy the folder path to the chrome driver.
Next search for and open the environment variables for your system. There are many ways to get to your environment variables but the easiest that I found is by going into the Windows search and typing in "path", than select the option for "Edit the system environment variables".
Select the Environment Variables button.
![system props.PNG]
()
There are two panes, one for User variables and on the bottom is System variables. We will want to past the path of the chrome driver to the system variable inside of path. Hover over the line for "Path" and click on it to highlight it. Than select the "Edit" button.
Next click on the "New" button, this will allow you to insert a new line in the list. Paste the copied path for the chrome driver. Than click "OK" on the open boxes to close them. FYI I know there is already another path in there for Chrome as it was from an earlier install, but note that you should only have one path per driver.
Next lets verify that the system is able to find and use our driver. If you still have your command prompt open. Close it out and re-open it. If you do not close and re-open your command prompt there is a good chance the system will not be able to find the driver. Once you have a new command prompt open type in "chromedriver.exe" and press enter. You should get a response in regards to starting chrome driver on port .... and that only local connections are allowed. Otherwise it will say something along the lines of not a valid command or not able to find the file ect.... May need to make sure the path you pasted is where the chromedriver.exe is actually located. Can also try restarting your machine to see if that helps.
Great, so at this point we are all set up with Selenium Webdriver and Python 3!!! Lastly we need a text editor to write our code in. You can use any text editor that you prefer but lately I have been using Microsoft's, Visual Studio Code(VSC) editor. I also recommend notepad++ and atom but with VSC you get a nice built in view of your project file and folder structure and built in command line. You may also hear about pycharm as it is designed around Python and has some similar features as VSC but for me personally I really like using VSC. You can google these but I will paste the URL's below.
Notepad++ https://notepad-plus-plus.org/
Atom https://atom.io/
VSC https://code.visualstudio.com/
PyCharm https://www.jetbrains.com/pycharm/
Super basic simple script time!
So open your text editor of choice and write the following line after line. If you have never written Python before, it is very specific with spacing and indentations and it will YELL at you until you fix it! Once you have written the code, save the file with a .py file extension. Than in your command prompt navigate to where the directory where the file is located and type "python test.py" to execute the script. FYI my files name is test.py yours may be different if you gave it another name.
If all is well you should have seen the Chrome browser open up and navigate to google.com and sit on the page for 5 seconds and than close. I just thought of this but I hope I would not have to actually tell someone this but if you don't have Chrome installed on your machine it will not work...... Don't be that person lol.
Alright guys and gals if you made it this far, thank you for taking the time to read my post. If it at least helps one person I will of accomplished my goal. I will create a more in depth script at a later time but I've got to get back to work! Hope everyone has a great day!
Congratulations @free-lance-wd! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Do not miss the last post from @steemitboard:
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit