ITishnik ArtStudio
ITishnik ArtStudio

Reputation: 37

Python selenium can't find element by id

I started to learn python selenium and have one small problem

>>> from selenium import webdriver
>>> dr = webdriver.Chrome('C:/Users/Dima/Desktop/chromedriver.exe')
>>> dr.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
>>> pole = dr.find_element_by_id('f213cb817d764e4')
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    pole = dr.find_element_by_id('f213cb817d764e4')
  File "C:\Users\Dima\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\selenium\webdriver\remote\webdriver.py", line 360, in 
find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\Dima\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\Dima\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
  File "C:\Users\Dima\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\selenium\webdriver\remote\errorhandler.py", line 242, in 
  check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: 
Unable to locate element: {"method":"id","selector":"f213cb817d764e4"}
  (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.45.615291 
(ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.17763 
x86_64)

What I can do? I do all from documentation but it don`t works.

Upvotes: 1

Views: 1539

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193088

The username field on Instagram is a React Native element so you have to induce WebDriverWait and then invoke send_keys() method as follows :

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("ITishnik_ArtStudio")

Note : You have to add the following imports :

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

Upvotes: 0

Andersson
Andersson

Reputation: 52665

@id is generated dynamically, so it will be different each time you run your script. If you want to select Username input field, try to select by @name:

dr.find_element_by_name('username')

Upvotes: 2

Related Questions