Morten Stulen
Morten Stulen

Reputation: 1333

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:

I'm trying to automatically generate lots of users on the webpage kahoot.it using selenium to make them appear in front of the class, however, I get this error message when trying to access the inputSession item (where you write the gameID to enter the game)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.kahoot.it")

gameID = driver.find_element_by_id("inputSession")
username = driver.find_element_by_id("username")

gameID.send_keys("53384")

This is the error:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:
{"method":"id","selector":"inputSession"}

Upvotes: 51

Views: 198651

Answers (9)

user20468552
user20468552

Reputation:

I had the same problem as you and this solution saved me:

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'someid')))

Upvotes: 1

Claudio Paladini
Claudio Paladini

Reputation: 1028

In my case, the error was caused by the element I was looking for being inside an iframe. This meant I had to change frame before looking for the element:

from selenium import webdriver
    
driver = webdriver.Chrome()
driver.get("https://www.google.co.uk/maps")

frame_0 = driver.find_element_by_class_name('widget-consent-frame')
driver.switch_to.frame(frame_0)

agree_btn_0 = driver.find_element_by_id('introAgreeButton')
agree_btn_0.click()

Reddit source

Upvotes: 7

Subh
Subh

Reputation: 4424

Looks like it takes time to load the webpage, and hence the detection of webelement wasn't happening. You can either use @shri's code above or just add these two statements just below the code driver = webdriver.Firefox():

driver.maximize_window() # For maximizing window
driver.implicitly_wait(20) # gives an implicit wait for 20 seconds

Upvotes: 57

kishmat
kishmat

Reputation: 11

Also for some, it may be due to opening the new tabs when clicking the button(not in this question particularly). Then you can switch tabs by command.

driver.switch_to.window(driver.window_handles[1]) #for switching to second tab

Upvotes: 1

a zEnItH
a zEnItH

Reputation: 137

It seems that your browser did not read proper HTML texts/tags, use a delay function that'll help the page to load first and then get all tags from the page.

driver = webdriver.Chrome('./chromedriver.exe')
# load the page
driver.get('https://www.instagram.com/accounts/login/')
# use delay function to get all tags
driver.implicitly_wait(20)
# access tag
driver.find_element_by_name('username').send_keys(self.username)

Upvotes: 2

lowlife
lowlife

Reputation: 11

this worked for me (the try/finally didn't, kept hitting the finally/browser.close())

from selenium import webdriver

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

driver = webdriver.Firefox()
driver.get('mywebsite.com')

username = None
while(username == None):
    username = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "username"))
    )
username.send_keys('[email protected]')

Upvotes: 1

Omry
Omry

Reputation: 1

it just means the function is executing before button can be clicked. Example solution:

from selenium import sleep
# load the page first and then pause
sleep(3)
# pauses executing the next line for 3 seconds

Upvotes: 0

shri046
shri046

Reputation: 1198

Could be a race condition where the find element is executing before it is present on the page. Take a look at the wait timeout documentation. Here is an example from the docs

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

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

Upvotes: 48

Tikkaty
Tikkaty

Reputation: 792

You can also use below as an alternative to the above two solutions:

import time
time.sleep(30) 

Upvotes: 3

Related Questions