SyntaxError: invalid syntax with executable_path in ipython

99封情书 提交于 2020-04-18 03:50:26

问题


I am using selenium web driver for an assignment in Python. I am getting a syntax error. I am using google colab and Python 3.

Here is my code

import time
from selenium import webdriver
driver = webdriver.Chrome (r "C:\Users\Anisha\Downloads\chromedriver.exe")
time.sleep(20)

I am getting error

File "<ipython-input-28-7654fa692ce2>", line 1
driver = webdriver.Chrome (r "C:\Users\Anisha\Downloads\chromedriver.exe")
                                                                        ^
SyntaxError: invalid syntax

Please help I am not getting where I am wrong.


回答1:


If you intend to pass the location of the chromedriver binary in Windows OS you have to:

  • While mentioning the absolute location of the chromedriver binary through the Key / Value pair of executable_path you have to add the binary extension as as well i.e. .exe.
  • While mentioning the absolute location of the chromedriver binary you have to either use the single front slash i.e. \ along with the raw r switch or you have to escape the back slash \\.
  • Your effective line of code will be :

    • Either in this format:

      driver = webdriver.Chrome(executable_path="C:\\Users\\Anisha\\Downloads\\chromedriver.exe")
      
    • Or in this format:

      driver = webdriver.Chrome(executable_path=r'C:\Users\Anisha\Downloads\chromedriver.exe')
      



回答2:


Do not leave spaces between raw string literals marker and the string:

r "String" --> r"String"

Use

r"C:\Users\Anisha\Downloads\chromedriver.exe"


来源:https://stackoverflow.com/questions/53365183/syntaxerror-invalid-syntax-with-executable-path-in-ipython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!