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

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(ADDED.exe):

Path and location of web driver

Modified code below based on feedback:

   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")

Error:

CMD



Solution 1:[1]

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')

Solution 2:[2]

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')

Solution 3:[3]

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'

Solution 4:[4]

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")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AlexanderK1987
Solution 2 Akzy
Solution 3 Software Tester at ExpandCart
Solution 4 undetected Selenium