What's the equivalent of selenium.focus()
for WebDriver?
element.sendKeys("");
or
new Actions(driver).moveToElement(element).perform();
I have tried both of them and they worked, but which one would always work on all elements?
Which one is the correct way for any elements (such as button, link etc.)? This matters to me because the function will be used on different UI's.
This question is related to
java
focus
selenium-webdriver
setfocus
You can use JS as below:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('elementid').focus();");
We can also focus webelement using below code:
public focusElement(WebElement element){
String javaScript = "var evObj = document.createEvent('MouseEvents');"
+ "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);"
+ "arguments[0].dispatchEvent(evObj);";
((JavascriptExecutor) getDriver()).executeScript(javaScript, element);
}
Hope it helps :)
C# extension method code, focus element, enter text, call change().
public static void EnterText(this IWebDriver driver, IWebElement element, string textToEnter)
{
var js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].focus();", element);
js.ExecuteScript("arguments[0].setAttribute('value', arguments[1])", element, textToEnter);
js.ExecuteScript("$(arguments[0]).change();", element);
}
Called by:
driver.EnterText(element, text);
The focus only works if the window is focused.
Use ((JavascriptExecutor)webDriver).executeScript("window.focus();");
to be sure.
This code actually doesn't provide focus:
new Actions(driver).moveToElement(element).perform();
It provides a hover effect.
Additionally, the JS code .focus() requires that the window be active in order to work.
js.executeScript("element.focus();");
I have found that this code works:
element.sendKeys(Keys.SHIFT);
For my own code, I use both:
element.sendKeys(Keys.SHIFT);
js.executeScript("element.focus();");
FWIW, I had what I think is a related problem and came up with a workaround: I wrote a Chrome Extension that did an document.execCommand('paste') into a textarea with focus on window unload in order to populate the element with the system clipboard contents. This worked 100% of the time manually, but the execCommand returned false almost all the time when run under Selenium.
I added a driver.refresh() after the initial driver.get( myChromeExtensionURL ), and now it works 100% of the time. This was with Selenium driver version 2.16.333243 and Chrome version 43 on Mac OS 10.9.
When I was researching the problem, I didn't see any mentions of this workaround, so I thought I'd document my discovery for those following in my Selenium/focus/execCommand('paste') footsteps.
Source: Stackoverflow.com