Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to focus on a element the right way?

Im doing webtest using selenium Webdriver in C#. But I'm having a problem where when the browser window isn't in full size a popup will open half way outside the visible area.

The problem is that when i fire a .Click(); it doesn't do anything because the link i attempt to click is outside of the viewed area.

So how do i focus on the link to get click to work? Im currently using the following workaround but i don't think that's a nice way.

        _blogPostPage.FindElement(By.XPath(_popupLogin)).SendKeys("");
        _blogPostPage.FindElement(By.XPath(_popupLogin)).Click();

The sendkeys with space focuses on the link and makes Click work everytime, but isn't there a right way to do it?

like image 461
Martin Mussmann Avatar asked Sep 08 '11 13:09

Martin Mussmann


People also ask

How do you set focus on a specific element in HTML?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.

How do you shift focus in Selenium?

Selenium driver object can access the elements of the parent window. In order to switch its focus from the parent to the new popup tab, we shall take the help of the switchTo(). window method and pass the window handle id of the popup as an argument to the method.


1 Answers

Instead of doing send key for blank value, send it for space. Thats the keyboard shortcut to select a checkbox.

Just replace the code :

_blogPostPage.FindElement(By.XPath(_popupLogin)).SendKeys("");
_blogPostPage.FindElement(By.XPath(_popupLogin)).Click();

by

_blogPostPage.FindElement(By.XPath(_popupLogin)).SendKeys(Keys.Space);
like image 85
Sandeep Avatar answered Sep 19 '22 23:09

Sandeep