简体   繁体   English

在python中使用Selenium chromedriver

[英]Using selenium chromedriver with python

I have installed python 2.7 and pip in my environment variables. 我已经在环境变量中安装了python 2.7和pip。 I have also installed selenium in my python path. 我还在python路径中安装了硒。 Now I am trying to create a selenium script using PyCharm. 现在,我正在尝试使用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. 我有三重检查webdriver路径等。我也尝试从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. 实例化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 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. ...表示Python脚本无法访问webdriver模块。

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 : 始终通过单反斜杠(即\\内的单引号(即'.....'以及裸机r开关,将Key executable_path 路径一起指定为ChromeDriver绝对路径 ,如下所示:

     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. 尝试以非root用户身份执行@Tests

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. Python将自动检测到它。

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. 诸如virtualenv之类的python库可用于此目的。

code snippet :- 代码段:-

def main():

    global driver

   # Create a instance of Chrome browser

    driver = webdriver.Chrome()

  call your function here

  # exit the browser

   driver.quit(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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