Correct way to focus an element in Selenium WebDriver using Java

The following code – element.sendKeys(“”); tries to find an input tag box to enter some information, while new Actions(driver).moveToElement(element).perform(); is more appropriate as it will work for image elements, link elements, dropdown boxes etc. Therefore using moveToElement() method makes more sense to focus on any generic WebElement on the web page. For an input box … Read more

Set keyboard focus to a

you can make a div focusable if you add a tabindex attribute. see: http://snook.ca/archives/accessibility_and_usability/elements_focusable_with_tabindex The tabindex value can allow for some interesting behaviour. If given a value of “-1”, the element can’t be tabbed to but focus can be given to the element programmatically (using element.focus()). If given a value of 0, the element can … Read more

Setting focus to iframe contents

I had a similar problem with the jQuery Thickbox (a lightbox-style dialog widget). The way I fixed my problem is as follows: function setFocusThickboxIframe() { var iframe = $(“#TB_iframeContent”)[0]; iframe.contentWindow.focus(); } $(document).ready(function(){ $(“#id_cmd_open”).click(function(){ /* run thickbox code here to open lightbox, like tb_show(“google this!”, “http://www.google.com”); */ setTimeout(setFocusThickboxIframe, 100); return false; }); }); The code doesn’t … Read more