Home > Blockchain >  Log in to instagram using selenium
Log in to instagram using selenium

Time:10-01

I am a novice coder. Trying my hand at creating an instagram bot and I'm following the instructions found here: (https://realpython.com/instagram-bot-python-instapy/)

I can fill in the elements for user name and password, but I'm having trouble clicking the login link. Using the -

#submit = browser.find_element_by_tag_name('form')
#submit.submit()
  • portion of my code works to log in, but I would like to be able to use find element by xpath and for this so I can apply it in different situations. If someone could please let me know where I can look in the HTML on the instagram page to find what I'm looking for, or direct me towards any helpful reading, I'd really appreciate it! I've tried a few ways to do this.

Here is my code. I also added photos of my code and error message.

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

#sets browser to firefox - need to make sure that geckodriver and firefox are in the path
browser = webdriver.Firefox()

#opens firefox to instagram#
browser.get('https://www.instagram.com/accounts/login/?source=auth_switcher')


#tells selenium to wait 5 seconds before trying again if it can't find something
browser.implicitly_wait(10)

print ('i waited')


user = 'username'
password = 'password'


##### USERNAME #####
username = browser.find_element_by_xpath("//input[@name='username']")
username.send_keys(user)

#username = browser.find_element_by_name('username')
#username.send_keys(user)

##### PASSWORD #####
password = browser.find_element_by_xpath("//input[@name='password']")
password.send_keys(password)

#password = browser.find_element_by_name('password')
#password.send_keys(password)

browser.implicitly_wait(10)

##### LOG IN #####

#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 = browser.find_element_by_tag_name('form')
#submit.submit()

#login_link = browser.find_element_by_xpath("//article/div/div/p/a[text()='Log in']")

login_link = browser.find_element_by_xpath("//a[text()='Log in']")
login_link.click()


#login_form = browser.find_element_by_xpath("//form[@id='loginForm']")
#form.submit()



print ('login incomplete :)')


sleep(5)

browser.quit()  #quits geckodriver and mozilla

print ('closed')

Picture of error

code part 1

code part 2

CodePudding user response:

Your xpath is bit wrong. you are using

//a[text()='Log in']

but Log in is inside div tag, not a tag.

Please use this xpath

//div[text()='Log In']/..

In your code

login_link = browser.find_element_by_xpath("//div[text()='Log In']/..")
login_link.click()

The HTML is

<div class="            qF0y9          Igw0E     IwRSH      eGOV_         _4EzTm                                                                                                              ">Log In</div>

and we are using text() to target text Log in and then we are looking for button using /.. which is a parent node.

CodePudding user response:

Replace this link

login_link = browser.find_element_by_xpath("//a[text()='Log in']")

with

login_link = browser.find_element_by_xpath("//div[contains(text(),'Log In')]")

And you should be good. Since the Login In button is wrapped inside a div element and not a element.

CodePudding user response:

As already explained Log In is in a div tag under a button tag with type submit. You can use this xpath too.

browser.find_element_by_xpath("//button[@type='submit']").click()
  • Related