Selenium Webdriver Throws Exception in Thread "Main" Org.Openqa.Selenium.Elementnotinteractableexception

Selenium WebDriver throws Exception in thread main org.openqa.selenium.ElementNotInteractableException

ElementNotInteractableException

As per the documentation, ElementNotInteractableException is the W3C exception which is thrown to indicate that although an element is present on the DOM Tree, it is not in a state that can be interacted with.

Reasons & Solutions :

The reason for ElementNotInteractableException to occur can be numerous.

  1. Temporary Overlay of other WebElement over the WebElement of our interest:

    In this case, the direct solution would have been to induce ExplicitWait i.e. WebDriverWait in combination with ExpectedCondition as invisibilityOfElementLocated as follows:

    WebDriverWait wait2 = new WebDriverWait(driver, 10);
    wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_element_to_be_invisible")));
    driver.findElement(By.xpath("xpath_element_to_be_clicked")).click();

    A better solution will be to get a bit more granular and instead of using ExpectedCondition as invisibilityOfElementLocated we can use ExpectedCondition as elementToBeClickable as follows:

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
    element1.click();
  2. Permanent Overlay of other WebElement over the WebElement of our interest :

    If the overlay is a permanent one in this case we have to cast the WebDriver instance as JavascriptExecutor and perform the click operation as follows:

    WebElement ele = driver.findElement(By.xpath("element_xpath"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", ele);

Now addressing the error ElementNotInteractableException in this particular context we need to add ExplicitWait i.e. WebDriverWait as follows :

You need to induce a bit of wait for the Password field to be properly rendered in the HTML DOM. You can consider configuring an ExplicitWait for it. Here is the working code to login into Gmail using Mozilla Firefox:

System.setProperty("webdriver.gecko.driver","C:\\Users\\Ruchi\\workspace2\\SeleniumTest\\jar\\geckodriver-v0.17.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement email_phone = driver.findElement(By.xpath("//input[@id='identifierId']"));
email_phone.sendKeys("error59878@gmail.com");
driver.findElement(By.id("identifierNext")).click();
WebElement password = driver.findElement(By.xpath("//input[@name='password']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(password));
password.sendKeys("test1");
driver.findElement(By.id("passwordNext")).click();

Exception in thread main org.openqa.selenium.ElementNotInteractableException: element not interactable

Can you try with the below updated xpath's, Hope this will resolve your issue

  System.setProperty("webdriver.chrome.driver","C:\\chromedriver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window();
driver.get("https://qa-advisor.cs105.force.com/s/login/");
Thread.sleep(2000);
WebElement username = driver.findElement(By.xpath
("//div[@id='sfdc_username_container']//child::div/input[1]"));
Thread.sleep(5000);
WebElement password = driver.findElement(By.xpath
("//div[@id='sfdc_password_container']//child::div/input[1]"));
WebElement login = driver.findElement(By.xpath
("//button[@class='slds-button slds-button--brand loginButton uiButton-
-none uiButton']//child::span"));
username.sendKeys("test");
password.sendKeys("test");
login.click();

Sample Image

org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard: while sending text to FirstName field in Facebook

ElementNotInteractableException: Element is not reachable by keyboard

Element is not reachable by keyboard in plain words means that the element can’t be reached using the keyboard, which means you won't even physically interact with it.

Reason

There can be multiple reasons behind the error Element is not reachable by keyboard which can be either of the following:

  • The element is hidden, as modern JavaScript-centric UI styles always keep the ugly raw HTML input field hidden. The hidden attribute could have been implemented through either of the following ways:
  • A temporary overlay of some other element over the desired element.
  • A permanent overlay of some other element over the desired element.
  • Presence of attributes e.g. class="ng-hide", style="display: none", etc
  • As per best practices while sending character sequence, you must not attempt to invoke click() or sendKeys() on any <p> or <div> tag; instead, invoke click() on the desired <input> tag following the Official locator strategies for the webdriver.

Solution

There are different approaches to address this issue.

  • Incase of temporary overlay, use WebDriverWait inconjunction with ExpectedConditions for the desired element to be visible/clickable as follows:
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.nsg-button"))).click();
  • Incase of permanent overlay, use executeScript() method from JavascriptExecutor interface as follows:
import org.openqa.selenium.JavascriptExecutor;

String inputText = "Rozmeen";
WebElement myElement = driver.findElement(By.id("u_0_b"));
String js = "arguments[0].setAttribute('value','"+inputText+"')"
((JavascriptExecutor) driver).executeScript(js, myElement);

You will find a detailed discussion in Using JS to enter text, but if I input text in one text box, the value already entered is getting deleted.

  • Incase presence of attributes e.g. class="ng-hide", style="display: none", etc., use executeScript() method from the JavascriptExecutor interface to edit and reset the style="display: none" attribute to style="display: block" as follows:
import org.openqa.selenium.JavascriptExecutor;
((JavascriptExecutor) driver).executeScript("document.getElementById('ID').style.display='block';");

You will find a detailed discussion in Can't fill in the Hidden text area element.

References

  • For input[type=file], should allow sendKeys even when display=none
  • Special-casing file upload controls in keyboard-interactability check
  • Element is not reachable by keyboard
  • Input field with display: none is not interactable at all?


This particular issue

If you look into the HTML of Facebook login page, the application contains React Native elements. So an element once represented with id as u_0_b in your system may not be represented by the same id as u_0_b in the next run on your system. Hence, we have to take the help of Dynamic Locator Strategy. You can use the following code block to perform your intended steps :

  • Code Block :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com");
driver.findElement(By.xpath("//input[@name='firstname' and contains(@class,'inputtext')]")).sendKeys("testing it ");
//DOB
Select sel1 = new Select(driver.findElement(By.xpath(".//*[@id='month']")));
sel1.selectByIndex(4);
Select sel2 = new Select(driver.findElement(By.xpath(".//*[@id='day']")));
sel2.selectByValue("6");
Select sel3 = new Select(driver.findElement(By.xpath(".//*[@id='year']")));
sel3.selectByValue("2013");
//clicking sign up
driver.findElement(By.xpath("//button[@name='websubmit' and contains(.,'Sign Up')]")).click();
  • Browser Client :

FacebookRegistration



Update

Addressing the error:

org.openqa.selenium.ElementNotInteractableException: Element is not
reachable by keyboard

has become easier with the availability of Firefox capability moz:webdriverClick

moz:webdriverClick()

Through webdriverClick(), you can pass a boolean value to indicate which kind of interactability checks to run when performing a click or sending keys to an element. For Firefoxen prior to v58.0, some legacy code as imported from an older version of FirefoxDriver was in use. With the availability of Firefox v58, the interactability checks as required by the WebDriver specification are enabled by default. This means that geckodriver will additionally check if an element is obscured by another when clicking and if an element is focusable for sending keys. Because of this change in behaviour, we are aware that some extra errors could be returned. In most cases, the test in question might have to be updated so it conforms with the new checks.

To temporarily disable the WebDriver conformant checks, use false as value for this capability.

Note: This capability exists only temporarily, and it will be removed once the interactability checks have been stabilized.

Selenium - Error: element not interactable

Please try any of following xpath

//label[contains(text(),'User ID')]/following-sibling::input

//input[@placeholder='User ID']

your xpath is targeting label. You need to locate <input>

org.openqa.selenium.ElementNotInteractableException: Element a class=bg-inverse could not be scrolled into view when trying to click a button

This error message...

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <a class="bg-inverse text-white dropdown-item" href="/admin/worker-summary"> could not be scrolled into view

...implies that the GeckoDriver / FirefoxDriver was unable to interact with the desired element.

Solution

Once you locate the element btnWorkerSummary moving ahead as you are invoking click() instead of ExpectedConditions as visibilityOf you need to use elementToBeClickable() as follows:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='/admin/worker-summary']"))).click();

However, another issue is the incompatibility between the version of the binaries you are using as follows:

  • Your Selenium Client version is 3.12.0.
  • Your JDK version is 1.8.0_51 which is ancient.

Solution

  • Upgrade JDK to recent levels JDK 8u201.
  • Execute your @Test.


Related Topics



Leave a reply



Submit