Home > front end >  SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3

Time:04-27

I'm confused as to what is causing this error:

The chrome version is 100.0.4986 which is the latest Google chrome version

Python version is 3.9.1: Python version

Chrome web driver version is 100.0: Chrome webdriver version

Path and location of web driver:

Path and location of webdriver

Code below and error:

from selenium import webdriver

driver = webdriver.Chrome('C:\Users\sanas\OneDrive\CSIT 110 Python\chromedriver')
driver.get("https://www.google.com")

Error:

CMD error

CodePudding user response:

Backslash \ is an escape character, so you have to use \\ Change your code to:

driver = webdriver.Chrome('C:\\Users\\sanas\\OneDrive\\CSIT 110 Python\\chromedriver')

CodePudding user response:

Solution:

Use the \\ in window and provide the complete path, you forget to add .exe

driver = webdriver.Chrome('C:\\Users\\sanas\\OneDrive\\CSIT 110 Python\\chromedriver.exe')

CodePudding user response:

You can use one of following :

driver = webdriver.Chrome('C:\\Users\\sanas\\OneDrive\\CSIT\\Python\\chromedriver.exe'

Or

driver = webdriver.Chrome('C:/Users/sanas/OneDrive/CSIT/Python/chromedriver.exe'

CodePudding user response:

This error message...

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

...implies that there was unicode escape error within the path of the chromedriver.


Solution

You need to modify a couple of things here as follows:

  • First and foremost, as you are on system you need to append the extension of the binary executable as well i.e. .exe
  • As you are using single backslashes i.e. \ you need to add the raw prefix r
  • Ideally you should add the key executable_path as well.

Your effective code block will be:

from selenium import webdriver

driver = webdriver.Chrome(executable_path=r'C:\Users\sanas\OneDrive\CSIT 110 Python\chromedriver.exe')
driver.get("https://www.google.com")
  • Related