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

元气小坏坏 提交于 2019-11-27 22:56:09

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

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

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/

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

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!

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()
Hemant Madan

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!