Test Execution in Chrome using Python Selenium
In this post, we are going to try test execution in Chrome browser and load URL of our choice.
Note: For Firefox Test Execution, Please check here
Method 1
Steps to be followed are same as Firefox. Only the instance will change from Firefox to Chrome
Step 1:
Import webdriver module. The selenium.webdriver module provides support to WebDriver implementations – Firefox, Google Chrome, Internet Explorer and RemoteWebDriver.
from selenium import webdriver
Step 2:
Create a Chrome driver instance
driver = webdriver.Chrome()
Step 3:
Load URL of our choice. WebDriver will wait until the page is loaded completely (the “onload” event has fired) before returning control to next line of script.
When there is AJAX calls on load, WebDriver may not know when page has completely loaded.
driver.get("http://www.google.com")
Final block of code:
from selenium import webdriver driver = webdriver.Chrome() driver.get("http://www.google.com")
Save this in a python file ‘OpenUrlInChromeBrowser.py’ and execute.
Note: Refer previous article for Firefox to set driver executable path.
To add driver folder path to environment path, use following command in Linux
export PATH=$PATH:<folderpath>
Example:
export PATH=$PATH:/data/WorkArea/BrowserDrivers
Method 2
There is another way to start Chrome browser without PATH settings. We have seen this for Firefox Browser
Instead of setting PATH environment variable, executable_path property can be passed while invoking Chrome instance.
Syntax:
driver = webdriver.Chrome(executable_path=”[path to driver location]”);
from selenium import webdriver driver = webdriver.Chrome(executable_path="/data/Drivers/chromedriver") driver.get("http://www.google.com")
3 Responses
Step 2 description should be: “Create a Chrome driver instance”.
Thank you for pointing out. Updated it.
Hope you are finding this blog useful. Please let us know if we can improve anything – http://allselenium.info/contact-us/
Great post.