Search code examples
selenium-webdriverselenium-chromedriverfindelement

How to find an "input" element using Selenium/Python3


I am trying to login into Instagram by using selenium and Python but I cant get find input elements like "uername" and "password". I am using Google Chrome

HTML

<label class="_aa48"><span class="_aa4a">Telefonnummer, Benutzername oder E-Mail-Adresse</span><input aria-label="Telefonnummer, Benutzername oder E-Mail-Adresse" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="75" class="_aa4b _add6 _ac4d" type="text" value="" name="username"></label>

I looked a lot of similar posts but I guess selenium got updates and none of the solutions are working in my case.

Following code I tried without success:

My Code (try 1)

enter_username = self.driver.find_element(By.NAME, "username")

My Code (try 2)

enter_username = self.driver.find_element(By.CSS_SELECTOR, "._aa48")


Solution

  • The input fields have attribute name. Can use NAME selector and below are the Xpath and CSS selector for the same.

    Xpath: 
    //input[@name='username']
    
    CSS:
    input[name='username']
    

    Try with below code. It has worked for me.

    # imports for Explicit waits
    
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver.get("https://www.instagram.com/accounts/login/")
    wait = WebDriverWait(driver,30)
    
    wait.until(EC.element_to_be_clickable((By.NAME,'username'))).send_keys("username")
    wait.until(EC.element_to_be_clickable((By.NAME,'password'))).send_keys("password")
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[type='submit']"))).click()