Home > database >  I have a problem in using (EC.presence_of_element_located(By.class, ""))
I have a problem in using (EC.presence_of_element_located(By.class, ""))

Time:11-28

I am doing Python selenium project and have a problem with

EC.presence_of_element_located

It looks like Python can't recognize it.

Here is my code:

service = Service("C:\\Users\\jjang\\.wdm\\drivers\\chromedriver\\win32\\96.0.4664.45\\chromedriver.exe")
browser = webdriver.Chrome(service=service)
browser.maximize_window()
browser.get("https://www.google.com")
actions = ActionChains(browser)
actions.send_keys(Keys.TAB * N)
actions.send_keys(Keys.ENTER)
actions.perform()
search_bar = browser.find_element(By.CLASS_NAME, "gLFyf")
search_bar.send_keys("hola")
search_bar.send_keys(Keys.ENTER)
unnecessary = WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.CLASS_NAME,"VjDLd mnr-c g-blk")))

And Python tells me

NameError: name 'EC' is not defined

Why does Python identify ES as a Name? Is there someone who can help me?

CodePudding user response:

In order to use EC, You need to import this

from selenium.webdriver.support import expected_conditions as EC

That should fix this error

NameError: name 'EC' is not defined

Also, I see You are using class name like this VjDLd mnr-c g-blk.

You will get NoSuchElement exception.

Please remove the space and make that a CSS selector instead, cause spaces for class name are not supported by Selenium.

So Instead of

unnecessary = WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.CLASS_NAME,"VjDLd mnr-c g-blk")))

Use this :

unnecessary = WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR,".VjDLd.mnr-c.g-blk")))

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  • Related