We are now in a good position to get started with the our first script. Open up PyCharm (or your IDE) and create first script, where we will be opening Google’s search page in Firefox.
from selenium import webdriver driver=webdriver.Firefox() driver.implicitly_wait(20) driver.maximize_window() driver.get('http://www.google.com') driver.quit()
Save this as
firstscript.py. Now it’s time to run the script.
Running Our First Script
If you’re using a Text Editor like the Sublime Editor, then follow these steps :
Open Terminal with Ctrl +Alt+ T.
Go to the folder where the created script is kept. For me it is
Run the script as
You’ll see the Firefox browser pop-up and then Google’s home page coming up. This is our target. We have achieved it. Mission Accomplished!!
If, you’re using an IDE, like PyCharm, then go to the RUN option at the top. Click on it.
Select the second one, as that is your test script’s name. The next screen will appear like this
And then, the browser window will come up and you’ll get the google home page
Understanding the Script
So we were successful in the creation of scripts using Selenium and Python. Now it’s time to understand how this worked actually.
from selenium import webdriver
The first line calls the Selenium bindings into the script, i.e. the webdriver bindings which are to be utilized.
Using this, we create the Firefox instance of Selenium webdriver.
Next line is an implicit wait for 20 seconds. This we will talk more about when we will implement webdriver waits.
Next we are maximizing the browser window.
Now we will browse to the website we want to go. The driver.get method will navigate to a page given by the URL. WebDriver will wait until the page has fully loaded (that is, the “onload” event has fired) before returning control to your test or script.
quit () method is one which will close the instance of the browser and exit it.
The above example is an example of script with Firefox. To see how we can run a script on Google chrome, click here.