16

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();
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
Janet
  • 251
  • 1
  • 2
  • 11
  • Oh yeah forgot to mention that the error occurs for the element for setting the active flag. That was working fine until I added the above code to assign the user to groups. – Janet Dec 30 '15 at 21:15
  • please refine the question. where are you setting activeFlag. error stacktrace can help. – parishodak Dec 31 '15 at 01:57
  • activeFlag is contained in a file that I am reading. All variables are set when I read the file and split the line. I will add the stack trace. – Janet Dec 31 '15 at 13:15

7 Answers7

58

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();
parishodak
  • 4,506
  • 4
  • 34
  • 48
  • 3
    Python, Selenium 3.8.1: `from selenium.webdriver.common.action_chains import ActionChains; actions = ActionChains(driver)` – julka Jan 16 '18 at 13:49
  • Worked for me. Thanks a lot for saving my day. – Ashok May 16 '18 at 15:56
  • @a_programmer glad it helped – parishodak May 18 '18 at 17:53
  • 1
    I've gotten this to work before with hard-to-reach buttons. But now I've got a case where it doesn't. The actions does absolutely nothing, and preduces no errors. – Frank H. Aug 22 '18 at 12:12
  • @FrankH. your comment doesn't give any background on the problem. if you think your problem is different situation and not able to find answers in previous questions, please create a new question on SO – parishodak Aug 22 '18 at 19:19
  • It did not help for me, I'm facing error in my Ionic based application, with chromer driver version 2.36. Any suggestions ? – Vasanth Feb 13 '20 at 07:04
3

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/

Valeriya
  • 31
  • 2
2

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!!

Janet
  • 251
  • 1
  • 2
  • 11
0

I Found very important code while reading "Action class" code.

"Action class" Works because of

actions.click()  

?!?

.

Try Just putting

element.click()

before

element.sendKeys()

in your existing code.

click() method makes element focused!!~~ THX for every senior

0

The chosen answer worked only partially for me. By adding

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(element));
element.clear();

before using the suggested answer, it worked completely!

Alex
  • 357
  • 2
  • 15
0

This is an edited version of the correct answer, as I was unable to just copy paste that code. With the code below you can copy paste it (If the element is found as id):

elem = driver.find_element_by_id("WHATEVER THE ELEMENT ID IS HERE")
actions = ActionChains(driver)
actions.move_to_element(elem)
actions.click()
actions.send_keys("PUT YOUR TEXT IN HERE")
actions.perform()
Romano
  • 619
  • 6
  • 12
0

May be the Xpath you have set is not up to that element level. E.g. if a text box is traversing through Div\div\textarea then most probably, you are taking only Div\ part. I had the same problem and it got resolved after writing xpath upto textarea node.

kk.
  • 3,747
  • 12
  • 36
  • 67