简体   繁体   中英

Using selenium chromedriver with python

I have installed python 2.7 and pip in my environment variables. I have also installed selenium in my python path. Now I am trying to create a selenium script using PyCharm. My simple code is this:

from selenium import webdriver

import time

driver = webdriver.Chrome(r"C:\Users\Path_to_driver\chromedriver.exe")

driver.set_page_load_timeout(40)

driver.get("http://www.facebook.com")
time.sleep(1)

driver.find_element_by_name("email").send_keys("abc@abc.com")
time.sleep(1)
driver.find_element_by_name("pass").send_keys("abcd")
time.sleep(1)
driver.find_element_by_id("loginbutton").click()

time.sleep(4)

driver.quit()

I am getting below error when I run the code. I have triple checked webdriver path etc. and I also tried running it from python IDLE. But I am getting error as shown below :

Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/test/Test/test1.py", line 5, in <module>
    driver = webdriver.Chrome(r"C:\Users\Path_to_driver\chromedriver.exe")
AttributeError: module 'selenium.webdriver' has no attribute 'Chrome'

I don't know what I am doing wrong. I read many articles on internet, but no solution seems to be solving my problem.

remove the path when you instantiate the webdriver. if it's in your path it will find it.

from selenium import webdriver
import time
driver = webdriver.Chrome() # Optional argument, if not specified will search path.
driver.set_page_load_timeout(40)

http://chromedriver.chromium.org/getting-started

This error message...

driver = webdriver.Chrome(r"C:\Users\Administrator\Desktop\arpit\automation\chromedriver_win32\chromedriver.exe")
AttributeError: module 'selenium.webdriver' has no attribute 'Chrome'

...implies that the Python Script was unable to access the webdriver module.

As per the best practices you need to follow the following points :

  • Always specify the Key executable_path along with the Value as the absolute path of the ChromeDriver through single back slash ie \\ within single quotes ie '.....' along with the raw ie r switch as follows :

     driver = webdriver.Chrome(executable_path=r'C:\\Users\\Administrator\\Desktop\\arpit\\automation\\chromedriver_win32\\chromedriver.exe') 
  • Try to execute your @Tests as a non-root user.

No need to explicitly provide the driver path in your code. just put the driver path in path environment variable as well. Python will automatically detect it.

Further, piece of advice always try and work in a virtual environment so that project installation does not interfere with global libraries.

python libraries like virtualenv can be used for this purpose.

code snippet :-

def main():

    global driver

   # Create a instance of Chrome browser

    driver = webdriver.Chrome()

  call your function here

  # exit the browser

   driver.quit(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM