Error "name 'by' is not defined" using Python Selenium WebDriver
Asked Answered
I

3

43

I keep getting an error as below:

NameError: name 'By' is not defined

for the code

chrome_driver_path = r"C:\chromedriver.exe"
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

browser = webdriver.Chrome(chrome_driver_path)
browser.delete_all_cookies()
browser.get("https://www.google.com/")
wait = WebDriverWait(browser, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="tsf"]/div[2]/div[3]/center/input[1]')))

Is my import correct?

Inhesion answered 19/6, 2017 at 11:57 Comment(1)
looks like you forgot to import itKuhn
I
109

You have to import it:

from selenium.webdriver.common.by import By
Intine answered 19/6, 2017 at 12:0 Comment(0)
E
18

You can import By by using:

from selenium.webdriver.common.by import By
Eveliaevelin answered 19/6, 2017 at 12:2 Comment(0)
M
2

Adding this line at the top of the code resolved my problem:

from selenium.webdriver.common.by import By
Mckinnon answered 5/10, 2022 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.