Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting cannot focus element in chrome and edge using java/selenium

I am getting a cannot focus element error when running my test in chrome and edge in FF it works fine. I have tried posted resolutions but to no avail. I am not sure what to do about this. Hoping someone out there can help. Thanks in advance.

driver.findElement(By.linkText("Add")).click();
List <WebElement> groups = new Select(driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/fieldset/div[3]/div[2]/div/div/div[1]/select"))).getOptions();
groups.get(3).click();
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement groupRole = driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/fieldset/div[3]/div[2]/div/div/div[2]/label[2]"));
js.executeScript("arguments[0].click();", groupRole);
driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/fieldset/div[3]/div[2]/div/div/div[2]/label[2]")).sendKeys(" ");
// Check to see if the user should be made active and set active checkbox to on if value in file is "active"
if (activeFlag.equals("active"))
{
    driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/fieldset/div[3]/div[1]/div[1]/div/div/input")).sendKeys(" ");
} 
// If the user role is to be admin then set the Site role to Administrator
if (userLevel.equals("admin"))
{
    List <WebElement> roles = new Select(driver.findElement(By.name("community_role"))).getOptions();
    roles.get(1).click();
}

Here is the stack trace:

 Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element
      (Session info: chrome=47.0.2526.106)
      (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 10.0 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 81 milliseconds
    Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
    System info: host: 'Janet-PC', ip: '192.168.56.1', os.name: 'Windows 8.1', os.arch: 'x86', os.version: '6.3', java.version: '1.7.0_80-ea'
    Session ID: 0a9dbd54a81a42a4178b25b95bb14f63
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities [{platform=WIN8_1, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\Janet\AppData\Local\Temp\scoped_dir7508_15608}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=47.0.2526.106, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, hasTouchScreen=false, applicationCacheEnabled=false, takesScreenshot=true}]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
        at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:326)
        at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:121)
        at Viddler_create_user.createUser.test(createUser.java:244)
        at Viddler_create_user.createUser.setUpBeforeClass(createUser.java:126)
        at Viddler_create_user.createUser.main(createUser.java:64)

Here is another stack trace. I am actually getting the error when trying to click the submit button not the active flag. I had already done the actions steps which I will also provide.

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element
  (Session info: chrome=47.0.2526.106)
  (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 10.0 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 52 milliseconds
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
System info: host: 'Janet-PC', ip: '192.168.56.1', os.name: 'Windows 8.1', os.arch: 'x86', os.version: '6.3', java.version: '1.7.0_80-ea'
Session ID: f332b496aa54d581c764f7328e770e65
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=WIN8_1, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\Janet\AppData\Local\Temp\scoped_dir2072_18936}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=47.0.2526.106, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, hasTouchScreen=false, applicationCacheEnabled=false, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:326)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:121)
    at Viddler_create_user.createUser.test(createUser.java:244)
    at Viddler_create_user.createUser.setUpBeforeClass(createUser.java:126)
    at Viddler_create_user.createUser.main(createUser.java:64)

Code:

        Actions actions = new Actions(driver);
        actions.moveToElement(driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/input[2]")));
        actions.click();
//      actions.sendKeys(" ");
        actions.build().perform();
like image 381
Janet Avatar asked Dec 30 '15 21:12

Janet


People also ask

How do you get focused elements in Selenium?

In Selenium 2.0, if you are using WebDriver to drive the tests in the browser, you can use the WebDriver. TargetLocator class to get the element in focus, in a window/frame: WebDriver driver = ... // initialize the driver WebElement currentElement = driver.

What could be the possible reason of getting unable to locate element?

We may encounter the error - unable to locate element while working with Selenium webdriver. This leads to NoSuchElementException. This type of exception is thrown when there is no element on the page which matches with the locator value. Check if there is any syntax error in our xpath expression.

How do you know if an element is focused in Selenium?

We can test if an element is focused with Selenium webdriver. This is determined with the help of the activeElement() method. First of all we need to identify the element with help of any of the locators like id, class, name, xpath or css.

What is contextClick () used for?

Move to Element: contextClick() method first performs mouseMove to the middle of the element location. This function performs the right click at the middle of the web element.

What are selenium focus element issues?

Or if it is on another iFrame, tab, or window which is not in focus, or any such scenarios. These types of unexpected erroneous outcomes are known as Selenium Focus Element issues. Selenium can only control a browser when its window is in focus. How do you ensure that the Selenium test code interacts with web elements when they are in focus?

Why is my Selenium WebDriver not focusing on any iframe?

This error occurs because the WebDriver instance is unable to focus on iFrame “classFrame” upon which the element com.thoughtworks.selenium is present. Similarly, in many such scenarios, when the instance’s focus is on some other iFrame, and the required element is on other, the Selenium Focus Element issues happen.

How to focus on a specific element in a webelement?

Therefore using moveToElement () method makes more sense to focus on any generic WebElement on the web page. For an input box you will have to click () on the element to focus. while for links and images the mouse will be over that particular element,you can decide to click () on it depending on what you want to do.

How do you ensure that selenium test code interacts with web elements?

Selenium can only control a browser when its window is in focus. How do you ensure that the Selenium test code interacts with web elements when they are in focus? You can use workarounds like the addition of minimal delay (in seconds) to ensure that the element on which the testing is performed has loaded.


3 Answers

sendkeys method is the problem as per the stack trace.

at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:121)

Please try Actions class to first focus on the element then send required keys.

Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.sendKeys("SOME DATA");
actions.build().perform();
like image 160
parishodak Avatar answered Oct 16 '22 12:10

parishodak


For future reference, if others run into this issue, make sure you're only finding one element! Chrome tools can be deceiving sometimes when it comes to this. I re-checked my selector in firePath (firefox add-on) and it turned out that I had two matching nodes, even though chrome tools showed me one element.

https://addons.mozilla.org/en-US/firefox/addon/firepath/

like image 26
Valeriya Avatar answered Oct 16 '22 13:10

Valeriya


The Actions resolution did work after all. I apparently had an extra driver.findElementBy line that should have been commented out as it was a duplicate to something I had moved to another location.

Thanks for your help!!

like image 2
Janet Avatar answered Oct 16 '22 12:10

Janet