Search code examples
c#selenium-webdriverfirefoxautomationwebdriver

Selenium c# OpenQA.Selenium.ElementNotInteractableException : Element <input id="PhoneNumber"


WebDriver can't seem to scroll into view of element in FireFox.

I'm trying to look for the element, clear the text box and send keys into the text field. This works fine on Chrome and Edge just FireFox I am experiencing this issue.

No matter what I try FireFox keeps failing and telling me it cannot scroll into view for the element

Currently I am searchign for the element, clearing the field and sending keys:

This is in C#

search.IdPhoneNumberField.Clear();
search.IdPhoneNumberField.SendKeys("21542585");

This is throwing me an error sayng this is not in view. I've tried the following:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("PhoneNumber")));
driver.FindElement(By.Id("PhoneNumber")).Clear();
driver.FindElement(By.Id("PhoneNumber")).SendKeys("21542585");
WebDriverWait whateverspace = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
whateverspace.Until(drv => drv.FindElement(By.Id("PhoneNumber")));
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
var element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("PhoneNumber")));

element.Clear();
element.SendKeys("21542585");
Actions act = new Actions(driver);
act.MoveToElement(search.IdPhoneNumberField).SendKeys("21542585").Perform();

No matter what I try FireFox keeps informing me the element is not in view to scroll to event through when I manually add a javascript scroll, it will scroll down to where I can see the element but FireFox refuses to find it.

var elem = driver.FindElement(By.Id("PhoneNumber"));
driver.ExecuteJavaScript("arguments[0].scrollIntoView(true);", elem);

Is there anyone who can help with this please?


Solution

  • I have come across browser-specific quirks with Firefox & Selenium before too, and I've encountered a similar scroll issue more specifically.

    One of our workarounds was utilising scrollIntoView which you've already mentioned, but that didn't always work either. We ended up implementing a js mouseover/customScroll event when the above failed:

    document.addEventListener('customScroll', e => {
        document.documentElement.scrollTop = e.target.offsetTop;
    });
    
    let scrollTo = document.getElementById('elementID');
    if (scrollTo) {
        scrollTo.dispatchEvent(new Event('customScroll', { bubbles: true, cancelable: true }));
    }
    

    So in C#, something like:

    string jsScript = @"
                      document.addEventListener('customScroll', e => {
                          document.documentElement.scrollTop = e.target.offsetTop;
                      });
    
                      arguments[0].dispatchEvent(new Event('customScroll', { bubbles: true, cancelable: true }));
                      ";
    
    driver.ExecuteJavaScript(jsScript, elementToScrollTo);
    

    I hope some variation of the above helps.