0

i want to login on instagram using selenium python. I tried to find elements by name, tag and css selector but selenium doesn't find any element (i think). I also tried to switch to the iFrame but nothing.

This is the error:

Traceback (most recent call last): File "C:/Users/anton/Desktop/Instabot/chrome instagram bot/main.py", line 8, in my_driver.sign_in(username=USERNAME, password=PASSWORD) File "C:\Users\anton\Desktop\Instabot\chrome instagram bot\chrome_driver_cli.py", line 39, in sign_in username_input = self.driver.find_element_by_name("username") File "C:\Users\anton\Desktop\Instabot\chrome instagram bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 496, in find_element_by_name return self.find_element(by=By.NAME, value=name) File "C:\Users\anton\Desktop\Instabot\chrome instagram bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element 'value': value})['value'] File "C:\Users\anton\Desktop\Instabot\chrome instagram bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\anton\Desktop\Instabot\chrome instagram bot\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="username"]"} (Session info: chrome=79.0.3945.130)

This is the code

from selenium import webdriver
from selenium.common.exceptions import SessionNotCreatedException
from selenium.webdriver.common.keys import Keys


supported_versions_path = [
            "..\\chrome driver\\CHROME 80\\chromedriver.exe",
            "..\\chrome driver\\CHROME 79\\chromedriver.exe",
            "..\\chrome driver\\CHROME 78\\chromedriver.exe"
        ]

instagram_link = "https://www.instagram.com/accounts/login/?source=auth_switcher"


class ChromeDriver:

    def __init__(self):
        self.driver = self.__startup()


    def __startup(self):

        self.driver = None
        for current_checking_version in supported_versions_path:
            if self.driver is None:
                try:
                    self.driver = webdriver.Chrome(current_checking_version)
                    pass
                except SessionNotCreatedException:
                    self.driver = None
        return self.driver

    def sign_in(self, username, password):

        self.driver.get(instagram_link)
        frame = self.driver.find_element_by_tag_name("iframe")
        self.driver.switch_to.frame(frame)

        username_input = self.driver.find_element_by_name("username")
        # password_input = self.driver.find_element_by_name('password')
        # username_input.send_keys(username)
        # password_input.send_keys(password)
        # password_input.send_keys(Keys.ENTER)

    def get_page(self, url):
        self.driver.get(url)

    def quit(self):
        self.driver.quit()

Can you help me?

2 Answers2

0

Seems you were close. The username field on Instagram is a ReactJS so you have to induce WebDriverWait and then invoke send_keys() using either of the following Locator Strategies:

  • CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("Antonio_Sabatino")
    
  • XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']"))).send_keys("Antonio_Sabatino")
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

The problem here seems to be a missing element which is 'username' but it must be there on login page. So what's the actual problem is that the driver opens up the login page and instantly search for the name 'username' where the page did not have completely loaded and hence giving the above error.

Try adding the delay before searching for elements in page for better working

self.driver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')  

time.sleep(3)

user = self.driver.find_element_by_name('username')
pasw = self.driver.find_element_by_name('password')

user.send_keys('your_username')
time.sleep(1)
pasw.send_keys('your_password')
time.sleep(3)
pasw.send_keys(Keys.RETURN)

the above worked for me and will work for anyone !!

vaibhav singh
  • 93
  • 1
  • 5