How to locate the username and password field within Instagram login page using Chromedriver and Selenium Python

后端 未结 4 1669
梦毁少年i
梦毁少年i 2021-01-26 21:50

Here\'s inspected source code

input aria-label=\"Phone number, username, or email\" aria-required=\"true\" autocapitalize=\"off\" autocorrect=\"off\" maxlength=\         


        
4条回答
  •  一整个雨季
    2021-01-26 22:18

    Instagram application is built through React elements. Hence just after invoking the url when you initiate the search for the login element, you face NoSuchElementException


    Solution

    To login within Instagram using a valid set of credentials you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:

    • Using XPATH:

      driver.get("https://www.instagram.com/")
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']"))).send_keys("username")
      driver.find_element_by_xpath("//input[@name='password']").send_keys("password")
      driver.find_element_by_xpath("//button/div[text()='Log In']").click()
      
    • 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
      

    Browser Snapshot:


    Reference

    You can find a couple of relevant discussions in:

    • Filling in login forms in Instagram using selenium and webdriver (chrome) python OSX
    • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium

提交回复
热议问题