bitschubser
bitschubser

Reputation: 66

webdriver can't log in to instagram

I'm working on an login script for instagram. It worked 3 times already, but now it's opening the chromedriver window with the correct url, but it doesn't insert the username and password. (I did no changes to the sourcecode in this time....)

When it worked first for about 3 times I've used the "....css_selektor". Now I've tried to place my inserts with the "....xpath", which i copied from the chrome-developer-window.

This is my code:

from selenium import webdriver

driver = webdriver.Chrome('my_path/chromedriver')
driver.get('https://www.instagram.com/accounts/login/')

#input_username = driver.find_element_by_css_selector("input[name='username']")
#password_input = driver.find_element_by_css_selector("input[name='password']")

input_username = driver.find_element_by_xpath("/html/body/div[1]/section/main/div/article/div/div[1]/div/form/div/div[1]/div/label/input")
password_input = driver.find_element_by_xpath("/html/body/div[1]/section/main/div/article/div/div[1]/div/form/div/div[2]/div/label/input")

input_username.send_keys("my_username")
password_input.send_keys("my_password")


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

Somebody have an idea? How I said, it worked about 3 times and then it doesn't insert my username and password. I didn't changed anything in this time....

Upvotes: 0

Views: 963

Answers (4)

ResulVlnzr
ResulVlnzr

Reputation: 31

Here is my code it should work

from instagramUserInfo import email, password
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


class Instagram:
    def __init__(self, email, password):
        self.browser = webdriver.Chrome()
        self.email = email
        self.password = password

    def signIn(self):
        self.browser.get("https://www.instagram.com/accounts/login")
        time.sleep(3)
        emailInput = self.browser.find_element_by_xpath(
            '//*[@id="loginForm"]/div/div[1]/div/label/input')
        passwordInput = self.browser.find_element_by_xpath(
            '//*[@id="loginForm"]/div/div[2]/div/label/input')
        loginInput = self.browser.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]/button')

        emailInput.send_keys(self.email)
        passwordInput.send_keys(self.password)
        loginInput.send_keys(Keys.ENTER)
        time.sleep(2)


instgrm = Instagram(email, password)
instgrm.signIn()

To make this code working first you need to create a file named instagramUserInfo.py in the same directory of your code then you need to write your username and password in it like below

username="Username"

password="password"

After this you can run code and autologin to instagram with a instabot:)

Upvotes: 0

James Cook
James Cook

Reputation: 344

I use driver.implicitly_wait(5) 5 is probably overkill but gets the job done. Try:

from selenium import webdriver

driver = webdriver.Chrome('my_path/chromedriver')
driver.get('https://www.instagram.com/accounts/login/')
driver.implicitly_wait(5)
input_username = driver.find_element_by_xpath("/html/body/div[1]/section/main/div/article/div/div[1]/div/form/div/div[1]/div/label/input")
password_input = driver.find_element_by_xpath("/html/body/div[1]/section/main/div/article/div/div[1]/div/form/div/div[2]/div/label/input")

input_username.send_keys("my_username")
password_input.send_keys("my_password")


login_button = driver.find_element_by_xpath("//button[@type='submit']")
login_button.click()

Here is a link to see how you can use different methods of asking selenium to wait https://selenium-python.readthedocs.io/waits.html#explicit-waits

Upvotes: 1

KUSHAGRA BANSAL
KUSHAGRA BANSAL

Reputation: 95

Sometimes due to the speed issues, the page cannot be loaded in time and selenium cannot perform the specified actions.
For such situations, you should use wait_time = webdriverWait(driver,time). In such cases try using the, wait_time.wait.until(EC.presence_of_element_located((By.XPATH,xpath))) For this, you should first import the following,
from selenium.webdriver.support import expected_conditions as EC.

Read more about webdriverWait() here

Also try to learn more about the error selenium is throwing, so accordingly you can use the expected_conditions.

Upvotes: 1

Revisto
Revisto

Reputation: 1249

Here is My Code

Drive = webdriver.Chrome()
Drive.get("https://www.instagram.com/accounts/login/")
sleep(5)
InputElements=Drive.find_elements_by_class_name("zyHYP")
UsernameInput=InputElements[0]
PasswordInput=InputElements[1]
UsernameInput.send_keys(str('UserName'))
PasswordInput.send_keys(str('Password'))
Click=Drive.find_elements_by_class_name("y3zKF")
Click[-1].click()
sleep(10)

Thanks

Upvotes: 1

Related Questions