1

I have this code:

IWebDriver driver = new FirefoxDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
//Go to the powerbi site
driver.Navigate().GoToUrl("https://powerbi.microsoft.com/en-us/");
//Go to the page with login form
driver.FindElement(By.XPath("html/body/div[2]/header/nav/div/ul[3]/li[1]/a")).Click();
//Fill in email field
driver.FindElement(By.XPath("//*[@id='cred_userid_inputtext']")).SendKeys("example@gmail.com");

When I launch this code on my computer everything works fine without errors. But when I launch this code on my boss's computer, sometimes it works and sometimes it doesn't.

When an error occurs, it's always on the last line of code. I don't remember exactly which error it is: InvalidElementStateException (when the target element is not enabled) or ElementNotVisibleException (when the target element is not visible).

I suppose the whole thing lies on the Click() method. The documentation says:

Click this element. If the click causes a new page to load, the Click() method will attempt to block until the page has loaded.

I don't quite understand how it attempts to block.

Sam
  • 1,222
  • 1
  • 14
  • 45
Zirochka
  • 146
  • 2
  • 10

2 Answers2

0

Seems like your input isn't always ready immediately after you click the button. You should wait for it before sending the keys:

Try this instead of your last line:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5));
var input = wait.Until(ElementIsClickable(By.XPath("//*[@id='cred_userid_inputtext']")));
input.SendKeys("example@gmail.com");

In order to do that check, you should create a ElementIsClickable function, which returns a delegate, just as this answer shows:

public static Func<IWebDriver, IWebElement> ElementIsClickable(By locator)
{
    return driver =>
    {
        var element = driver.FindElement(locator);
        return (element != null && element.Displayed && element.Enabled) ? element : null;
    };
}

Also, keep in mind that for using the WebDriverWait class you might need to import the Selenium.WebDriver and Selenium.Support packages into your project, just as this answer suggests.

Community
  • 1
  • 1
Sam
  • 1,222
  • 1
  • 14
  • 45
  • thanks, but for me this function (ElementIsClickable) didn't work. I use ExpectedConditions.ElementToBeClickable() function and here's code which works fine:`WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30)); var signIn = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("xpath"))); signIn.Click();` I apologize for appearance of the code above :) – Zirochka Mar 27 '17 at 11:30
0

It's coming occasional because sometimes it takes time to load element from DOM and which cause exception like InvalidElementStateException or ElementNotVisibleException. I ran your snippets code. It runs fine, try to separate action and find element.

WebElement element = driver.findElement(By.xpath(".//xyz"/);
element.sendkeys("data");

This could work.

promitheus
  • 67
  • 2
  • 13