Getting started with Python & Selenium

For those coming from Microsoft world. Python tool sets are available to develop in Visual Studio. Recently I was working on a situation where I have check a particular website every couple of hours. I did not know at I have to do it for some time, so first time I did not manually and then second time when I did, I know I have to code it. I remember someone told me Python is available in Visual Studio so I thought I will try python in Visual Studio.

Install:

First thing we need to do is to install python tools. When you start a new project in Visual Studio, pick python and if it is not installed, it will ask you to install python tool set and acknowledge it and install them. It normally take couple of mins to install them.

Now that we have python installed, open VS again and create a new python project. We normally use PIP to install python packages if we were using traditional python programming. We need to do the same in VS environment as well. Here is the stackoverflow question and answer. I followed the first answer and able to install the packages I wanted.

There is a possibility you might find ‘pip’ in there, make sure you follow this comment

@cal97g When the panel is smaller, you just have to change the dropdown menu from “Overview” to “pip”. I just expanded it in the picture to show it all at once

Now that we have pip. Select ‘install selenium’ and that’s it. It will install selenium and we are all set to go.

Selenium:

Now that we have all the pre-requisite. Let’s jump in and start coding. For my testing, I started out with the example from selenium-python documentation. I used the code as it is to see how it works before I start experimenting with it.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

I used the above code the chapter 2. Now with no other changes, copy the code in the python file and try to run it. There is a possibility you will run into this error

“selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable
needs to be in PATH.”

We can solve this by getting the latest version of geckodriver from here and adding it to the system PATH. Before you continue, restart the VS so that it has latest path.

Lets try again, now if the path was picked up properly, it should open the firefox. In case you run into an error like this

“Message: Expected browser binary location, but unable to find binary in default location, no ‘moz:firefoxOptions.binary’ capability provided, and no binary flag set on the command line”

It is because it does not find FireFox in the Path. It can be solved couple of ways. One way is use explicit path of Firefox in the code like the example here. But I like the solution of adding firefox.exe to the system path.