Selenium 2.0b3 IE WebDriver,点击不激活

dje*_*eeg 41 webdriver internet-explorer-9 selenium-webdriver

在IE9中使用IE驱动程序时,偶尔Click方法只会选择一个按钮,它不会执行Click()的操作.请注意,这只会偶尔发生,所以我不认为这是代码问题.在Firefox4上使用Firefox驱动程序没有问题.我也有一个问题,偶尔也没有找到元素,但只在IE中再次发现,而不是Firefox.

if (Driver.FindElement(By.Name("username")) == null) {
    //sometimes gets here in IE, never gets here in Firefox
}
Driver.FindElement(By.Name("username")).SendKeys(username);
Driver.FindElement(By.Name("surname")).SendKeys(surname);
Driver.FindElement(By.Name("firstname")).SendKeys(firstname);
string url = Driver.Url;
Driver.FindElement(By.Name("cmd")).Click();
if (Driver.Url == url) {
    //if the page didnt change, click the link again
    Driver.FindElement(By.Name("cmd")).Click();
}
Run Code Online (Sandbox Code Playgroud)

我已经看到了类似的问题(http://stackoverflow.com/questions/4737205/selenium-webdriver-ie-button-issue),但我没有动态生成的ID.

Nai*_*shy 27

我在Internet Explorer 8上尝试使用单击链接时发现了相同的内容.Click()- 即使我可以看到Selenium单击链接.根据我的经验,如果浏览器没有焦点,那么初始点击不起作用.

解决方法是将一个.Click()元素发送到页面上的另一个元素,以便在尝试单击链接之前浏览器获得焦点,例如它的父级:

Driver.FindElement(By.Id("Logout")).FindElement(By.XPath("..")).Click();
Driver.FindElement(By.Id("Logout")).Click();
Run Code Online (Sandbox Code Playgroud)

  • 我遇到了一个问题,我无法点击我选择的元素的父级.点击相同的元素两次工作,但我担心如果窗口已经有焦点,它可能会导致不必要的行为.这个对所选解决方案的微小改动对我有用:(在C#中)`driver.FindElement(By.TagName("body")).Click();` (8认同)

Sim*_*mon 15

我发现IE驱动程序错误,我的代码的相同版本在具有相同版本的IE的不同机器上表现不同.

为了在每个动作之前保持一致,我执行以下操作.

   driver.SwitchTo().Window(driver.CurrentWindowHandle);//Force Focus
Run Code Online (Sandbox Code Playgroud)

对我来说,这使得IE驱动程序的行为更符合预期.

  • 昂贵与否这远远优于作为公认答案的廉价黑客。 (2认同)

小智 8

遇到同样的问题,点击不适用于我的IE浏览器.我找到了一个解决方法,我在其中执行Driver.FindElement(By.Name("...")).sendKeys("\n")来执行单击(基本上我只需按下按钮上的Enter键).不是很干净,但它可以解决问题!


Bri*_*hey 7

我在我的项目中使用Selenium WebDriver 2.15.0并使用Selenium WebDriver Server 2.16.0重构了我的解决方案,并且我做了以下观察:

  • 使用时,click事件正确触发FirefoxDriver
  • 单击事件正确某些控制用火时RemoteWebDriverDesiredCapabilities.Firefox
  • 使用for 和时,click事件正确触发RemoteWebDriverDesiredCapabilities.HtmlUnitDesiredCapabilities.HtmlUnitWithJavaScript
  • InternetExplorerDriverRemoteWebDriverDesiredCapabilities.InternetExplorer(真正的同样的事情)仍给我,我发现很难明确不一致的结果.

我的前三个点的解决方案是创建自己的类,扩展RemoteWebDriverRemoteWebElement,这样我可以隐藏该继续引用测试代码我的自定义行为IRemoteWebDriverIWebElement.

我已经低于我目前的"调整",但是如果你使用这些自定义类,你将能够调整你的驱动程序和web元素行为到你的心脏内容,而无需更改你的测试代码.

public class MyRemoteWebDriver : RemoteWebDriver
{
    //Constructors...

    protected override RemoteWebElement CreateElement(string elementId)
    {
        return new MyWebElement(this, elementId);
    }
}

public class MyWebElement : RemoteWebElement, IWebElement
{
    //Constructor...

    void IWebElement.Click()
    {
        if (Settings.Default.WebDriver.StartsWith("HtmlUnit"))
        {
            Click();
            return;
        }

        if (TagName == "a")
        {
            SendKeys("\n");
            Thread.Sleep(100);
            return;
        }

        if (TagName == "input")
        {
            switch (GetAttribute("type"))
            {
                case "submit":
                case "image":
                    Submit();
                    return;
                case "checkbox":
                case "radio":
                    //Send the 'spacebar' keystroke
                    SendKeys(" ");
                    return;
            }
        }

        //If no special conditions are detected, just run the normal click
        Click();
    }
}
Run Code Online (Sandbox Code Playgroud)


And*_*rak 7

上述解决方案均不适合我.这样就可以了:

Java的

element.sendKeys(org.openqa.selenium.Keys.CONTROL);
element.click();
Run Code Online (Sandbox Code Playgroud)

Groovy的

element << org.openqa.selenium.Keys.CONTROL
element.click()
Run Code Online (Sandbox Code Playgroud)

创业板

或者,如果您使用Geb,那么有一个更好的解决方案是完全不显眼的:

(用IE7和Geb 0.7.0测试)

abstract class BaseSpec extends geb.spock.GebSpec
{
    static
    {
        def oldClick = geb.navigator.NonEmptyNavigator.metaClass.getMetaMethod("click")
        def metaclass = new geb.navigator.AttributeAccessingMetaClass(new ExpandoMetaClass(geb.navigator.NonEmptyNavigator))

        // Wrap the original click method
        metaclass.click = {->
            delegate << org.openqa.selenium.Keys.CONTROL
            oldClick.invoke(delegate)
        }

        metaclass.initialize()

        geb.navigator.NonEmptyNavigator.metaClass = metaclass
    }
}

class ClickSpec extends BaseSpec
{
    def "verify click"()
    {
        given:
        to HomePage

        expect:
        waitFor { at HomePage }

        when:
        dialog.dismiss()
        // Call the wrapped .click() method normally
        $('#someLink').click()

        then:
        waitFor { at SomePage }
    }
}

class HomePage extends geb.Page
{
    static url = "index.html"
    static at = { title == "Home - Example.com" }
    static content = {
        dialog { module DialogModule }
    }
}

class SomePage extends geb.Page { ... }
class DialogModule extends geb.Module { def dismiss() { ... } }
Run Code Online (Sandbox Code Playgroud)

在我的情况下,只要关闭动画模式叠加(我们正在使用jQuery工具叠加模式对话框),点击IE7似乎就会失败.上面的Geb方法解决了这个问题.