1

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("[email protected]")
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.

2
  • 2
    If that is a real password, you should probably change it ASAP Commented May 10, 2018 at 19:00
  • 1
    @DeliriousLettuce OMG bro thanks for pointing it out. It could have been a blunder! Commented May 11, 2018 at 16:48

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 i.e. \ within single quotes i.e. '.....' along with the raw i.e. 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.

Comments

0

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(

5 Comments

Why would you use a global variable though?
so that you can use the driver variable in your function as well.
I think you are missing the point, global variables should be avoided and there is no need to use one here.
yeah you can say in a way but in my usecase it was satisfying the need. nevertheless, you can declare on top a you wish. Hope the solution worked for you.
Unnecessary global variable aside, your solution doesn't even run. Also, I didn't ask the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.