0

I am getting this error and can't solve it. Image of the error

This is all the code. I heard adding waiting time might solve the issue but not sure how to add it.

IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.instagram.com");

IWebElement userName = driver.FindElement(By.Name("username"));
IWebElement password = driver.FindElement(By.Name("password"));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Engathin
  • 3
  • 1
  • Please post the actual error text and not a linked screenshot. It will help people find your issue more effectively down the road. – Mark Thormann Nov 19 '20 at 13:16

3 Answers3

0

You could call the Until method from an instance of a WebDriverWait class, which you could tell to wait until it finds, say, element username. For example:

var wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(10000));
wait.Until(x => x.FindElement(By.Name("username")));

Hope that helps, and if you're crawling Instagram is for the right purpose :)

dsosa
  • 71
  • 5
0

The identify the username field on Instagram login page you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:

  • Using ExpectedConditions:

    IWebDriver driver = new ChromeDriver();
    driver.Navigate().GoToUrl("https://www.instagram.com");
    IWebElement userName = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("username")));
    
  • Using lambda expression:

    IWebDriver driver = new ChromeDriver();
    driver.Navigate().GoToUrl("https://www.instagram.com");
    IWebElement userName = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(x=> x.FindElement(By.Name("username"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Your issue is not necessarily your code 100%. It really depends on how you are hitting the site. The way I access IG is to login from another site. When I lick login to IG I get the IG popup. Then I need to:

-switch to the popup

-Login

-Handle all of the other questions

-then switch to main

You may need to tweak this based on what you are doing but this should work.

    public void InstagramLogin()
    {
        Driver.FindElement(By.XPath("//button[@class='uppy-DashboardTab-btn']//div[contains(text(), 'Instagram')]")).Click();
        Driver.FindElement(By.XPath("//button[contains(text(), 'Connect to Instagram')]")).Click();
        Driver.SwitchToPopup();
        Driver.WaitForElementDisplayed_byXPath("//input[@name='username']"));
        Driver.FindElement(By.XPath("//input[@name='username']")).SendKeys("MyEmail@gmail.com");
        Driver.FindElement(By.XPath("//input[@name='password']")).SendKeys("myPassword");
        Driver.FindElement(By.XPath("//button[@type='submit']")).Click();
        Driver.WaitForElementDisplayed(By.XPath("//button[text()='Not Now']"));
        Driver.FindElement(By.XPath("//button[text()='Not Now']").Click();
        bool ele = Driver.IsElementPresent(By.XPath("//button[contains(text(), 'Continue')]"));
        if (ele)
        {
            Driver.FindElement(By.XPath("//button[contains(text(), 'Continue')]")).Click();
            Driver.WaitForElementNoLongerDisplayed_byXPath("//button[contains(text(), 'Continue')]");
        }
        Driver.GoToMainHandle();
    }
Dazed
  • 1,527
  • 1
  • 13
  • 25