问题
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 rawrswitch 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