1

i'm currently testing the GUI of my application, and i wanted to know if it's possible to set the focus to a WebElement ?

I'm using Selenium 2.0 and the webdriver.

So i'm looking for something like that : driver.findElement(xxxx).setfocus();

Thank you.

EDIT : I've alreardy tested that kind of tricky things

// getting the element WebElement 
eSupplierSuggest = driver.findElement(By.xpath("..."));

//get le location and click 
Point location = eSupplierSuggest.getLocation();
new Actions(driver).moveToElement(eSupplierSuggest, location.x, location.y).click();

//or

//directly perform
new Actions(driver).moveToElement(eSupplierSuggest).click().perform();

i red somewhere that the click focus the element, but in my case, nothing works. lol

PS : this is a sub-question of that original post Click on a suggestbox, webdriver

Community
  • 1
  • 1
e1che
  • 1,241
  • 1
  • 17
  • 34
  • using any framework? or just html css js application – daemonThread Apr 03 '13 at 08:44
  • @daemon : yes, i'm using Selenium 2, i'll add it on the question. My bad. – e1che Apr 03 '13 at 08:54
  • Can you let me know, why you need focus? Is it something similar to autosuggestion box that you are testing? – HemChe Apr 03 '13 at 09:31
  • @HemChe, as i said in the PS. I want to select an option in the suggestbox and validate by a click on it. I can found the element and the option, but when i click on it, nothing happens, so i wanted to do with other method like, set the focus on and sendkey(keys.ENTER). – e1che Apr 03 '13 at 09:38
  • See also: http://stackoverflow.com/questions/11337353/correct-way-to-focus-an-element-in-selenium-webdriver-using-java – Alexander Taylor Jun 21 '16 at 00:46

5 Answers5

6

I normally send an empty key to the element so it gets focused. So for example:

element.send_keys ""
Nora
  • 1,432
  • 8
  • 9
2

In order to set focus on the element you can use executeScript method as described below :

JavascriptExecutor js;
js.executeScript ("document.getElementById('x').focus()");

Once the focus is set you can easily use send_keys provided by webdriver api.

Mandy
  • 433
  • 2
  • 8
  • 25
0

Try using cssSelector for the autosuggestion click as shown below and let me know if you are still facing the issue.

 // supplier ops, i find and type data into the input
    WebElement eSupplier = driver.findElement(By.id("supplier:supplierOps_input"));
    eSupplier.sendKeys("OPS1");
    sleep(5); // wait the suggestbox

    // i find the suggestbox
    WebElement eSupplierSuggest = driver.findElement(By.cssSelector("css path of that specific value in the auto suggestion box"));
    eSupplierSuggest.click();
    sleep(5); // wait the refresh for the next field supplierAddress
HemChe
  • 2,249
  • 1
  • 21
  • 33
0

There is no function in the WebDriver API to set focus on an element.

If you want to do it you would have to write some JavaScript to set focus and then use a JavaScriptExecutor to run the JavaScript.

Ardesco
  • 7,281
  • 26
  • 49
0

Make sure, that you are not changing the frame.... Other wise .click() should do the trick

buddy
  • 183
  • 6