I'm new to code and I started creating an Instagram bot. My code open the webpage and navigate to the sign in page but doesn't fill in the blanks for the username and password. I wanted to use selenium, I've tried different method and what I upload here was the last one. I don't know what is the problem here.
from selenium import webdriver
import os
import time
class InstaBot:
def __init__(self, username = None, password = None):
self.username = '******'
self.password = '******'
self.driver = webdriver.Firefox()
self.login()
def login(self):
self.driver.get('https://instagram.com')
username_input = self.driver.find_element_by_name('username')
password_input = self.driver.find_element_by_name('password')
username_input.send_keys(self.username)
password_input.send_keys(self.password)
login_btn.click()
if __name__ == '__main__':
ig_bot = InstaBot('temp_username', 'temp_password')
CodePudding user response:
Hopefully, the problem solved. I've changed the code into
username_input = self.driver.find_element_by_css_selector("input[name='username']") password_input = self.driver.find_element_by_css_selector("input[name='password']")
and it works!!
CodePudding user response:
Instagram website is built of React element so to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("limbalu") self.driver.find_element(By.CSS_SELECTOR, "input[name='password']").send_keys("limbalu")
Using XPATH:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']"))).send_keys("limbalu") self.driver.find_element(By.XPATH, "//input[@name='password']").send_keys("limbalu")
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC