Deepu
Deepu

Reputation: 31

Why Selenium is giving me NoSuchElementException error

I have this piece of code:

path_web = "D:/chromedriver.exe"

insta = webdriver.Chrome(path_web)

insta.get("https://www.instagram.com/")

wait = WebDriverWait(insta, 20)

insta.find_element(By.TAG_NAME, "input")

When I run this I get NoSuchElementException error. But with the same code when I try to do this using cmd. I don't get the error. I get the element very easily. What am I doing wrong here?? why this code is only working on cmd?

Upvotes: 1

Views: 38

Answers (1)

Ratmir Asanov
Ratmir Asanov

Reputation: 6459

You are using not correctly the explicit wait. Here is the working code:

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


insta = webdriver.Chrome()
insta.get("https://www.instagram.com")

WebDriverWait(insta, 20).until(EC.element_to_be_clickable(
    (By.TAG_NAME, "input")))

PS: FYI, there are 2 inputs on the page. You need to locate them differently:

...
inputs = WebDriverWait(insta, 20).until(EC.visibility_of_all_elements_located(
    (By.TAG_NAME, "input")))
print(inputs[0])  # Username input.
print(inputs[1])  # Password input.

I hope it helps you!

Upvotes: 1

Related Questions