0

I want to enter a login, username and then click the button login in Instagram. Here is my code (Problem is below):

from selenium import webdriver
from time import sleep
from secrets import pw

class InstaBot():
    def __init__(self, username, password):
        self.driver = webdriver.Safari()
        self.driver.get("https://instagram.com")
        sleep(2)

        self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input')\
            .send_keys(username)
        sleep(3)
        self.driver.find_element_by_xpath("//input[@name=\'password\']")\
            .send_keys(password)
        self.driver.find_element_by_xpath("//button[@type=\'submit\']")\
            .click()
        sleep(4)

InstaBot('myusername', 'mypassword')

However, every time I run it, this happens:enter image description here

I have no idea why, but it writes my username, goes to the password, enters a couple of digits of the password, and then returns to the username and places the rest of the password there. I am clueless on as of why this is happening.

7
  • 3
    Try Selenium: selenium-python.readthedocs.io Commented Jul 4, 2020 at 19:16
  • 2
    Do you have a default browser?, if not try to put one. Commented Jul 4, 2020 at 19:17
  • 1
    @formicaman if he just wants to open a website, then there's no point on using Selenium, which ends up being overkill. webbrowser.open should work if he haves a default browser. Otherwise, he could use webbrowser.get first to get a controller for a specific browser Commented Jul 4, 2020 at 19:38
  • I now have another problem... I'm trying to input a username and password using selenium, can you guys help me? I can update the question Commented Jul 4, 2020 at 19:55
  • @yyyyyyyan and formican Commented Jul 4, 2020 at 19:56

1 Answer 1

1
def __init__(self, username, password):
    self.driver = webdriver.Safari()
    self.driver.get("https://instagram.com")
    sleep(2)

    self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input')\
        .send_keys(username)
    sleep(3)
    self.driver.find_element_by_xpath("//input[@name=\'password\']")\
        .send_keys(password)
    self.driver.find_element_by_xpath("//button[@type=\'submit\']")\
        .click()
    sleep(5)
    self.driver.find_element_by_xpath("//*[@class='sqdOP yWX7d    y3zKF     ']").click()
    sleep(4)
Sign up to request clarification or add additional context in comments.

7 Comments

Hey! Can you please give me a simple explanation of what I did wrong?
Can you add this line: self.driver.find_element_by_xpath("//*[@class='sqdOP yWX7d y3zKF ']").click()
Can you probably use Firefox?
Hey! It didn't work... But I created a loop to go over each character. but now I have another issue. It doesn't click the final log in button
Can you use another browser?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.