Selenium Chrome driver - SyntaxError: (unicode error) 'unicodeescape' codec

≯℡__Kan透↙ 提交于 2021-02-05 06:35:27

问题


I am trying to type my first GUI test in pycharm with selenium.

I installed selenium by cmd raport:

C:>pip install selenium Requirement already satisfied: selenium in c:\users\admin\appdata\local\programs\python\python37-32\lib\site-packages (3.141.0) Requirement already satisfied: urllib3 in c:\users\admin\appdata\local\programs\python\python37-32\lib\site-packages (from selenium) (1.24.1)

Then I wrote some code in pycharm:

from selenium import webdriver
import time
driver = webdriver.Chrome("C:\Users\Admin\Desktop")
driver.get("https://www.youtube.com/watch?v=FFDDN1C1MEQ");

And that is what happened when I clicked start :

File "C:/Users/Admin/PycharmProjects/untitled/venv/test.py", line 3 driver = webdriver.Chrome("C:\Users\Admin\Desktop") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

win.10 I don't know what I am doing wrong. Thanks for any help and have a good day.

edit: Yes, thanks it works. I have another error now:

C:\Users\Admin\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/Admin/PycharmProjects/untitled/venv/test.py
Traceback (most recent call last):
  File "C:/Users/Admin/PycharmProjects/untitled/venv/test.py", line 1, in <module>
    from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'

Process finished with exit code 1

回答1:


Backslashes in Python are escaping characters.
When you are going to use Windows path's make sure to use a raw string, to prevent Python trying to escape the string:

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



回答2:


As you can see in the error the problem is in line 3.

What you need is to specify the path to your chromedriver.exe

Just change it to driver = webdriver.Chrome("C:\your path to chromedriver\chromedriver.exe")

As for your second issue, it seems you don't have selenium installed:

Just run pip install selenium in your CMD (you need pip too)

For installing pip see here.

For installing selenium see here.

Hope you find this helpful!



来源:https://stackoverflow.com/questions/53429642/selenium-chrome-driver-syntaxerror-unicode-error-unicodeescape-codec

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