Reputation: 2044
While trying to learn more about Python, I tried to write a program that would, when given a link, perform an action on the web. I am using the selenium Module, and from it webdriver. The current code looks like this:
from selenium import webdriver
def supremeTest():
browser = webdriver.Firefox()
browser.get('http://www.google.com')
linkElem = browser.find_element_by_name("q")
type(linkElem)
linkElem.click()
The problem, however, lies in that when I change webdriver.Firefox()
to .Chrome()
the program opens a Chrome page, then exits before any action is completed.
I am wondering why this is so, and I am using python3, and I do have the latest version of ChromeDriver. I have looked at the help section of ChromeDriver, but from what I was looking at, there was nothing of help there.
Upvotes: 2
Views: 4005
Reputation: 1057
This is because selenium by default provide the driver for firefox but not for chrome. if you look at the method calls firefox can be called with 0 arguments.
webdriver.Firefox()
but not chrome method ,it will take a argument called executable_path
webdriver.Chrome(executable_path='<path>')
so, to run chrome from selenium you need to download the chrome webdriver from here. and keep the file path in the it will work fine. Since, you already downloaded the webdriver, please specify the path in the method call of chrome().Like this
path_to_chromedriver = 'C:/python34/chromedriver/chromedriver.exe'
browser2 = webdriver.Chrome(executable_path = path_to_chromedriver)
Hope it helps.
Upvotes: 3