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?

Answers


We've been playing with Selenium and have run into this problem as well. I don't know if it's the WebDriver as a whole, the C# implementation, the version of Firefox etc, but we have found an ok workaround:

The trick is to force Selenium to evaluate the LocationOnScreenOnceScrolledIntoView property on the RemoteWebElement class (which is inherited by FirefoxWebElement and implements IWebElement). This forces the browser to scroll so that the element is in view.

The way we've done it is to use an extension method:

using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace Namespace
{
    public static class ExtensionMethods
    {
        public static IWebElement FindElementOnPage(this IWebDriver webDriver, By by)
        {
            RemoteWebElement element = (RemoteWebElement)webDriver.FindElement(by);
            var hack = element.LocationOnScreenOnceScrolledIntoView;
            return element;
        }
    }
}

this way all we have to do is change the generated code from:

driver.FindElement(By.Id("elementId")).Click();

to:

driver.FindElementOnPage(By.Id("elementId")).Click();

Hope it works for you!?


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

driver.find_element(:id, "edit-section").send_keys " " with the space worked for me.

I am using webdriver rspec with selenium-server-2.24.1 and I was having trouble with IE8 - I kept getting Selenium::WebDriver::Error::ElementNotVisibleError. It was working in IE9 and FF but not IE8 until I added send_keys " ".


Need Your Help

NHibernate multi query / futures with Oracle

.net oracle nhibernate orm fluent-nhibernate

I am trying to use futures in NHibernate 3.2 and Oracle 11gR2. This doesn't seem to be supported although I'm not sure. I found this issue on NHibernate Jira that makes it seem like futures are

How do i add space(margin-bottom ) between the li's of my ul?

css css3

Hi i am trying to add some space below each li of my ul. When i try to set margin-bottom on the li it does not work it just adds margin above the first li element of the list. Can someone tell me h...