4

I want to log in to instagram using selenium, but I can't seem to enter values into the fields.

Here's my script:

#go to this address
browser.get('https://www.instagram.com')

#sleep for 1 seconds
sleep(1)

#find the 'login' button on homepage
login_elem = browser.find_element_by_xpath(
'//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a')

#navigate to login page
login_elem.click()

Having trouble from here onwards:

#locate the username field within the form
unform = browser.find_element_by_xpath(
'//*[@id="f3b8e6724a27994"]')

#clear the field
textunform.clear()

#enter 'test' into field
unform.send_keys('test')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
anon
  • 159
  • 2
  • 3
  • 9

9 Answers9

5

There is a trick in this, instead of searching for the Button (Log In) there is a better way to log in without it. how? let's see:

Import the packages you need:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
#Select the driver, In our case we will use Chrome.
chromedriver_path = 'chromedriver.exe' # Change this to your own chromedriver path!
webdriver = webdriver.Chrome(executable_path=chromedriver_path)
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)
username = webdriver.find_element_by_name('username')
username.send_keys('yourUsername')
password = webdriver.find_element_by_name('password')
password.send_keys('yourPassword')
#instead of searching for the Button (Log In) you can simply press enter when you already selected the password or the username input element.
submit = webdriver.find_element_by_tag_name('form')
submit.submit()

You can copy the code and run it directly (even without a real username or password) To get the webdriver (chromedriver.exe) from ChromeDriver

Karam Qusai
  • 713
  • 12
  • 16
3

The instagram is applying some method to leave the dynamic id, xpath and css, every time a reload happens on the page the attributes change their values, being more difficult to click or to set values:

enter image description here

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • 3
    `submit = webdriver.find_element_by_tag_name('form')` then `submit.submit()` This will solve the problem Check this answer: https://stackoverflow.com/a/57513626/10407102 – Karam Qusai Jan 13 '20 at 14:49
0

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

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

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
browser.get('https://www.instagram.com')
login_elem = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a')
login_elem.click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("anon")

Browser Screenshot :

BrowserScreenshot

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

I solved it:

#Locate the username field
unform = browser.find_element_by_name("username")
#Locate the password field
pwform = browser.find_element_by_name('password')

ActionChains(browser)\
    .move_to_element(unform).click()\
    .send_keys('test')\
    .move_to_element(pwform).click()\
    .send_keys('test')\
    .perform()

#Locate login button
login_button = browser.find_element_by_xpath(
'//*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/span/button')

#Click login button
login_button.click()
anon
  • 159
  • 2
  • 3
  • 9
0

In this case IMHO it's better to use this: browser.find_element_by_name("Bermil18") / browser.find_element_by_name("1q56y3w5t9k0p3es8i1q")

gmds
  • 19,325
  • 4
  • 32
  • 58
0

Here's my solution for Sign In on Instagram

def login(self, username, password):
    """ Methods that log in to Instagram by taking user's credentials as parameters"""
    self.driver.get("https://www.instagram.com/accounts/login/")
    try:            
        self.driver.find_element_by_xpath("//input[@name=\"username\"]").send_keys(username) # filling username
        self.driver.find_element_by_xpath("//input[@name=\"password\"]").send_keys(password) # filling password
        self.driver.find_element_by_xpath("//button[@type=\"submit\"]").click()              # submit form
    except NoSuchElementException:
        print("Failed to log in: Unable to locate Username/Password/LogIn element(s)")

    # If login is unsuccessful, Instagram will show a message "Sorry, your password was incorrect. Please double-check your password."
    success = self.driver.find_elements_by_xpath("//p[@id = \"slfErrorAlert\"]")
    if len(success) == 0:
        print("Login successful!")
    else: 
        print("Sorry, sign in unsuccessful. Please double-check your credentials.")

See my Github repo for more: https://github.com/mlej8/InstagramBot

Michael Li
  • 11
  • 2
0

def login(username,password):

driver.get(base_url)

time.sleep(3)
detail = driver.find_elements_by_class_name('_2hvTZ')
detail[0].clear()
detail[1].clear()
detail[0].send_keys(username)
detail[1].send_keys(password)
driver.find_element_by_class_name('L3NKy').click()
time.sleep(3)
for i in driver.find_elements_by_tag_name('button'):
    if i.text=='Not Now':
        i.click()
        break
time.sleep(3)
driver.find_element_by_class_name('HoLwm').click()

base url is intagram url . I have a made an instabot and you can find the code for logging in ,follow, unfollow ,like ,check posts in recent day ,etc in the following github link. https://github.com/Devanshchowdhury2212/Instagram-Web-scraping-

devansh
  • 89
  • 2
  • 8
0

This worked for me:

def login(self, username):
    self.driver = webdriver.Chrome()   
    self.driver.get('https://www.instagram.com/')
    sleep(1)
    username_input = self.driver.find_element_by_xpath(
        "//input[@name='username']")
    username_input.send_keys(username)
    password_input = self.driver.find_element_by_xpath(
        "//input[@name='password']")
    password_input.send_keys(pw)
    submit_btn = self.driver.find_element_by_xpath(
        "//button[@type='submit']")
    submit_btn.click()

    sleep(2) 
    save_your_login_info_not_now = self.driver.find_element_by_xpath("/html/body/div[1]/section/main/div/div/div/div/button")
    save_your_login_info_not_now.click()

You will notice that i am sending the variable pw instead of my actual password. This is for security reasons. Make a new file called secrets.py and inside it, declare your password in the following format:

pw = '*********'
d1sh4
  • 1,710
  • 5
  • 21
-1

Try to select the field with

unform = browser.find_element_by_xpath("//input[@name='username']")
unform.send_keys(<username>)

and for password

browser.find_element_by_xpath("//input[@name='password']")
BcK
  • 2,548
  • 1
  • 13
  • 27
  • In this case IMHO it's better to use `browser.find_element_by_name("username")` / `browser.find_element_by_name("password")` – Andersson Apr 20 '18 at 09:54
  • If there are any other name attributes outside of the input tags that might be less accurate. – BcK Apr 20 '18 at 09:56
  • this is better, now I'm in the form. Still can't seem to input any text into the field though. Thanks – anon Apr 20 '18 at 09:59
  • *Still can't seem to input any text...*. Any exception or input fields are empty? – Andersson Apr 20 '18 at 10:01
  • input fields are empty. The cursor starts to flash inside the field, but the .send_keys() method does not work. – anon Apr 20 '18 at 10:37