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

5

46

The error is:

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard

The code is:

System.setProperty("webdriver.gecko.driver","//Users//rozali//Documents//Selenium//geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.facebook.com");
driver.manage().window().maximize();

//entering first name
driver.findElement(By.id("u_0_b")).click();
driver.findElement(By.id("u_0_b")).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.id("u_0_t")).click();
Wolsky answered 16/4, 2018 at 19:35 Comment(2)
as your error states, driver.findElement(By.id("u_0_b")).sendKeys("testing it ") looks like it's not interactable. Go through your script step by step and see if it's showing, and accessible. Is there an element overlaying it?Marjorie
At first step it is showing the same error. I have check each line one by one but same error is visible every time.Wolsky
S
66

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.

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();
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


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.

Steamship answered 17/4, 2018 at 7:24 Comment(6)
Hi, how about chromedriver please? I've tried above webdriver.until(ExpectedConditions.elementToBeClickable), presentOfElement etc, but no luck. I also tried JS view.Physician
Maybe disabling CSS would be easier?Ozzie
Temporary or permanent overlays will not get you a ElementNotInteractableException. You will get the error message that talks about tried to click on X but Y element was in the way. Most of this answer is incorrect or contains misleading information.Mcgarry
@Mcgarry I think you are a bit confused here. I would suggest you to have a look at the Browser Architecture and how CSS works. That would clear your basic doubts. Let me know if you need further help on any of those topics or directions.Steamship
Nope... not confused at all. When you set up the scenario described here and try it for yourself, you'll see that it's not correct too.Mcgarry
i had style.display='none' and this code helped me: ((JavascriptExecutor) driver).executeScript("document.getElementById('ID').style.display='block';"); thanks a lot !Drawback
C
2

You can try this code:

public class Rozmeen{
    
    static WebDriver driver;
    static WebDriverWait wait;
    
    public static void main(String[] args) throws InterruptedException {
            System.setProperty("webdriver.gecko.driver", "F:\\Automation\\geckodriver.exe");
            driver = new FirefoxDriver();
            driver.manage().window().maximize();
            WebDriverWait wait = new WebDriverWait(driver, 40);
            driver.get("http://www.facebook.com");
            
            //entering first name
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("pagelet_bluebar"))));
            driver.findElement(By.name("firstname")).sendKeys("testing it ");
            
            //DOB
            selectFromDropDown(driver.findElement(By.name("birthday_day")), "4");
            selectFromDropDown(driver.findElement(By.name("birthday_month")), "Jun");
            selectFromDropDown(driver.findElement(By.name("birthday_year")), "2013");
            
            //clicking sign up
            wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name("websubmit"))));
            driver.findElement(By.name("websubmit")).click();
        }

       
        
        public static void selectFromDropDown(WebElement element , String Visibletext){
            Select select = new Select(element);
            select.selectByVisibleText(Visibletext);
        }
}  

try out this code and let me know the status.

Creationism answered 17/4, 2018 at 5:59 Comment(1)
i tried using the wait commands but still it was not working.Wolsky
P
1

In one of the use cases I had the same issue:

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <div id="search"> is not reachable by keyboard

using id for identifying an element before sending keys. Something like:

driver.findElement(By.id("search")).sendKeys("...");

After testing I changed to CSS Selector and it solved the issue:

driver.findElement(By.cssSelector("#search > input:nth-child(2)")).sendKeys("...");

So, I highly recommend to use different methods to interact with the elements, because something else can save your time solving problems.

Provision answered 2/8, 2019 at 21:41 Comment(0)
A
0

Sometimes it is also an issue by using name/id like this:

driver.findElement(By.name("phone")).sendKeys("99999999");

Instead of this, use Xpath. This solved the issue for me:

driver.findElement(By.xpath("//*[@id='load_form']/input")).sendKeys("Java");
Abettor answered 14/12, 2022 at 17:46 Comment(0)
G
-1

I got a similar error on a button when the click() operation was performed:

org.openqa.selenium.ElementNotInteractableException Exception

As mentioned by DebanjanB in his answer, the element was not reachable by keyboard. To solve this, I replaced click() with sendKeys(Keys.ENTER), and the button got clicked.

Guilbert answered 7/6, 2020 at 21:23 Comment(2)
No: pressing a keyboard key is a different thing to clicking a mouse button - that't the whole point. Check carefully if the (simulated) input device doesn't matter!Hejaz
@Hejaz In my case, the simulated input device didn't matter, only the CTA had to be invoked.Guilbert

© 2022 - 2024 — McMap. All rights reserved.