1

I am trying to find and then fill in the username and password of Instagrams login page using:

from selenium.webdriver.common.keys import Keys
import os
from selenium import webdriver
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'Chromedriver')
driver = webdriver.Chrome(executable_path = filename)
driver.get("https://www.instagram.com")
username = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")

username.send_keys("blankspace")
password.send_keys("blankspace")

however, I keep getting an error that it cant detect the element even though the name is correct

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="username"]"}

The HTML:

<input aria-label="Phone number, username, or email" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="75" name="username" type="text" class="_2hvTZ pexuQ zyHYP" value="">
3
  • 1
    @Joel This question does not refer to getting the source at all. Commented Dec 28, 2021 at 23:57
  • Try to debug the HTML source the driver sees docs. It is highly probable that Instagram detects you aren't human and is asking you some captcha before you can view the page. Because your code is sound, otherwise. Commented Dec 29, 2021 at 0:04
  • @edd you seem to be right, its not asking for a captcha but the page loads most of the elements after you get to the page so I added a time.sleep(5) and then tried to find the elements and it works. Commented Dec 29, 2021 at 17:14

3 Answers 3

1

You have to close the cookie consent popup first before accessing the form fields:

import os
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


dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'Chromedriver')
driver = webdriver.Firefox()
driver.get("https://www.instagram.com")

WebDriverWait(driver, 5).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, "button.aOOlW.bIiDR"))
)
driver.find_element(By.CSS_SELECTOR, "button.aOOlW.bIiDR").click()

username = driver.find_element(By.NAME, "username")
password = driver.find_element(By.NAME, "password")

username.send_keys("blankspace")
password.send_keys("blankspace")

driver.close()

Also consider using the updated methods find_element():

DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instea
Sign up to request clarification or add additional context in comments.

1 Comment

This might be the answer for some people that I don't have a cookie pop up. You were right about needing to have something that waits a couple seconds before searching for the elements.
0

I would check out Selenium documentation! Link. By going off the docs you could try the following:

username = browser.find_element(By.NAME, "username")
password = browser.find_element(By.NAME, "password")
username.send_keys("text")
password.send_keys("text")

Though I would use XPATH instead as it would be quicker:

username = browser.find_element(By.XPATH, '//[@id="loginForm"]/div/div[1]/div/label/input')
password = browser.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[2]/div/label/input')
username.send_keys("text")
password.send_keys("text")

Enjoy!

Comments

0

The answer to anyone who cares to know is that Instagram loads in the body of the page after you get to it so you just have to add a delay before you start looking for the elements. Thats to @edd for pointing that out

driver.get("https://www.instagram.com")
time.sleep(5)
username = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")

username.send_keys("blankspace")
password.send_keys("blankspace")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.