Filling in login forms in Instagram using selenium and webdriver (chrome) python OSX
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 webdriverfrom selenium.webdriver.common.keys import Keysfrom 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
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:
I solved it:
#Locate the username fieldunform = browser.find_element_by_name("username")#Locate the password fieldpwform = 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 buttonlogin_button = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/span/button')#Click login buttonlogin_button.click()