Nicolas F
Nicolas F

Reputation: 515

How can I open a website in my web browser using Python 3?

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.

Upvotes: 0

Views: 73

Answers (1)

Wadim
Wadim

Reputation: 26

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)

Upvotes: 1

Related Questions