Selenium 2.0b3 IE WebDriver,点击不激活

时间:2011-04-07 01:13:43

标签: internet-explorer-9 webdriver 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();
}

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

18 个答案:

答案 0 :(得分:27)

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

对此的解决方法是将.Click()发送到页面上的其他元素,以便在尝试点击链接之前浏览器获得焦点,例如这是父母:

Driver.FindElement(By.Id("Logout")).FindElement(By.XPath("..")).Click();
Driver.FindElement(By.Id("Logout")).Click();

答案 1 :(得分:15)

我发现IE驱动程序错误,并且我的代码的相同版本在具有相同版本IE的不同计算机上表现不同。

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

   driver.SwitchTo().Window(driver.CurrentWindowHandle);//Force Focus

对我而言,这使IE驱动程序的行为更符合预期。

答案 2 :(得分:8)

遇到同样的问题,点击无法使用我的IE浏览器。我找到了一个解决方法,我在其中执行Driver.FindElement(By.Name(“...”))。sendKeys(“\ n”)来执行单击(基本上我只需按下按钮上的Enter键)。不是很干净,但在问题解决之前一直有效!

答案 3 :(得分:7)

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

  • 使用FirefoxDriver
  • 时,点击事件正确触发
  • 使用RemoteWebDriver <{li}的DesiredCapabilities.Firefox时,点击事件无法正确触发某些控件
  • 使用RemoteWebDriverDesiredCapabilities.HtmlUnit
  • DesiredCapabilities.HtmlUnitWithJavaScript时,点击事件正确触发
  • InternetExplorerDriverRemoteWebDriverDesiredCapabilities.InternetExplorer(实际上是同一件事)仍然给我不一致的结果,我发现很难确定。

我对前三点的解决方案是创建我自己的扩展RemoteWebDriverRemoteWebElement的类,以便我可以从继续引用{{1}的测试代码中隐藏自定义行为}和IRemoteWebDriver

我已经低于我目前的“调整”,但是如果你使用这些自定义类,你将能够调整你的驱动程序和web元素行为,而不必更改你的测试代码。

IWebElement

答案 4 :(得分:7)

以上解决方案均不适合我。这就是诀窍:

爪哇

element.sendKeys(org.openqa.selenium.Keys.CONTROL);
element.click();

Groovy的

element << org.openqa.selenium.Keys.CONTROL
element.click()

盖布

或者,如果您使用的是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() { ... } }

就我而言,只要关闭动画模式叠加层(我们正在使用jQuery Tools Overlay Modal Dialog),点击IE7就会失败。上面的Geb方法解决了这个问题。

答案 5 :(得分:2)

尝试设置互联网选项 - &gt;安全 - &gt;启用保护模式所有区域的相同设置,请参阅http://www.mail-archive.com/watir-general@googlegroups.com/msg13482.html。这是来自Watir googlegroup,但在我的Selenium 2测试中,IE按钮点击似乎在应用此功能后效果更好。

答案 6 :(得分:1)

我使用的是IE版本:9 面临同样的问题。以下是我的案例

element.sendKeys(Keys.ENTER);
element.click();

答案 7 :(得分:1)

PHPUnit + facebook / php-webdriver 有时函数click()不会检查复选框元素。

我的解决方案是:

$Element = $WebDriver->findElement(
    WebDriverBy::id('checkbox_id')
);

if(false === $Element->isSelected())
{
    $Element->sendKeys(WebDriverKeys::SPACE);
}

答案 8 :(得分:1)

另一个:

v2.29.0

的webdriver:   * Firefox 18支持。   * IEDriver支持“requireWindowFocus”所需的功能。什么时候     使用此和本机事件,IE驱动程序将需要焦点和     用户交互将使用SendInput()来模拟用户     互动。请注意,这意味着你不能使用     在测试运行时,机器运行IE以获取其他任何内容。

答案 9 :(得分:1)

绝对没有其他事情适合我。 一些InternetExplorerDriver click()s正在为我工​​作,有些则没有。 然后我发现我错过了the documentation中的一行:浏览器的缩放级别必须设置为100%。

我确信所有其他答案都是指缩放级别已经达到100%的情况,但它确实解决了我的问题。所以先检查一下。

答案 10 :(得分:0)

经过一番搜索后,我发现了两件似乎对可重复测试有帮助的事情:

首先我添加了ImplicitlyWait 5秒。不确定这是否适用于所有FindElement函数,但我已经停止获取我得到的大部分NoSuchElementException。

OpenQA.Selenium.IE.InternetExplorerDriver driver = new OpenQA.Selenium.IE.InternetExplorerDriver();
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 0, 5, 0));
//driver.Manage().Speed = Speed.Medium;

其次我遇到了Logout功能问题并将代码更改为:

public LoginPageObject Logout() {
    Driver.FindElement(By.LinkText("Logout")).Click();

    OpenQA.Selenium.Support.UI.IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(Driver, TimeSpan.FromSeconds(5));
    IWebElement element = wait.Until(driver => driver.FindElement(By.Name("username")));

    LoginPageObject lpage = new LoginPageObject(Driver);
    return lpage;
}

显式等待似乎处理了ImplicitlyWait没有捕获的内容(我认为是因为重定向)。

http://code.google.com/p/selenium/source/browse/trunk/support/src/csharp/webdriver-support/UI/WebDriverWait.cs?r=10855

答案 11 :(得分:0)

我下一步解决了问题.click()。我使用JS和executeScript(JS,WebElement el)而不是.click() 示例:

protected void clickForIE(WebElement element){  
        ((JavascriptExecutor)wd).executeScript("var tmp = arguments[0];
          tmp.click()", element);  
    }

但在使用此方法后,我们应该在页面加载时等待。这就是我使用下一个方法的原因:

protected synchronized void pageWaitLoad() {  
        String str = null;
        try {
            str = (String)((JavascriptExecutor)wd).executeScript("return document.readyState");
        }
        catch (Exception e) {
// it's need when JS isn't worked
            pageWaitLoad();
            return;
        }
        System.out.println("ttt " + str);
        while(!str.equals("complete")){
            try {
                Thread.currentThread().sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            str = (String)((JavascriptExecutor)wd).executeScript("return document.readyState");
        }
    }

每次clickForIE()后都必须调用pageWaitLoad()。

答案 12 :(得分:0)

我的解决方案是:

Selenium WebDriver 2.29.0(JAVA),使用FF16和IE9测试

在制作findElement之前,我做了一个maxime浏览器屏幕。它工作正常。

public void maximeBrowser() {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenResolution = new Dimension((int)toolkit.getScreenSize().getWidth(), (int)toolkit.getScreenSize().getHeight());

        //Maximize the browser
        logger.info("Maximizing the browser : getWidth ["+screenResolution.getWidth()+"] - getHeight ["+screenResolution.getHeight()+"]");
        getDriver().manage().window().maximize();
    }

答案 13 :(得分:0)

强制关注元素的更好方法是使用Javascript。当您的元素使用id属性标记时,此方法有效。如果他们不是,请让开发人员改变它。

使用您需要的任何定位器/属性查找元素。检索元素后,检查它是否包含ID属性。如果是,则执行以下代码,该代码将强制关注元素:

JavascriptExecutor executor = (JavascriptExecutor) webDriver();
executor.executeScript("document.getElementById('" + element.GetAttribute("id") + "').focus()");

使用此功能时,使用InternetExplorerDriver时几乎解决了所有丢失点击的问题。

答案 14 :(得分:0)

在每次Click:

之前,我使用SendKeys作为解决方法,其中包含空字符串
element.SendKeys("");
element.Click();

答案 15 :(得分:0)

我正在使用2.0rc2,IE8,Java体验这一点。我实现可以发送多次点击的解决方案时遇到的问题是,有时它 工作。在那些情况下,单击我的对象两次不会让我的测试的其余部分向前移动。发送“Enter”键击对我们的控件也不起作用。

对此有一个类似的问题logged,但我的对象不一定靠近“视点”。任何更多的建议将不胜感激。

答案 16 :(得分:0)

简短回答:
如果您在IE11中运行自动Selenium测试并在触摸屏显示器(例如Windows 8触摸笔记本电脑)上打开浏览器窗口,请尝试在浏览器窗口中打开测试<强>非触摸屏。
原始的.click()方法应该可以在没有所有代码解决方法的情况下正常工作。

答案背景:
我在QA团队中与测试人员一起调查了类似的问题。在这里和selenium webdriver IE button issue尝试了大部分代码解决方案之后,我们最终发现问题只发生在测试人员的Windows 8笔记本电脑触摸屏上的IE(我们的版本11)中。 在外部戴尔监视器上运行IE窗口运行Selenium测试,每次都可以正常运行测试,即使只使用标准的.click()调用。 我们也在Magnific Popup(http://dimsemenov.com/plugins/magnific-popup/)对话框中的按钮点击事件失败。

我的假设:IE11(不确定其他版本)如何处理触摸屏上鼠标点击事件的触摸事件转换是一个问题。

答案 17 :(得分:0)

<强> WebdriverJS

IE中,当您尝试执行click()操作时,URL会在状态栏上保持闪烁。这意味着驱动程序正在关注元素并尝试执行click()操作。为了完成click()动作,我在每次点击动作之前和之后都使用了sleep()方法。

试试这个例子。

var webdriver = require('..'), By = webdriver.By, until = webdriver.until;
var driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'ie' }).build();

driver.get('http://www.google.com')
.then(function(){
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(1000 * 3);
    driver.findElement(By.name('q')).sendKeys('webdriver');
    driver.findElement(By.name('btnG')).then(function(button){
    button.click();
    });
})
.then(function(){
    driver.findElement(By.css('div[class="gb_Zb"] a[title="Google Apps"]')).then(function(apps){
        apps.click();       
        driver.findElements(By.css('a[class="gb_O"]')).then(function(appsList){
        console.log("apps : "+appsList.length);
        for(var i = 0; i < appsList.length; i++){
        console.log('applications : '+i);
        if(i == 5) {
        var element = appsList[i];
        driver.sleep(1000 * 5);
        driver.executeScript("var tmp = arguments[0]; tmp.click()", element);
        driver.sleep(1000 * 5);
        } } })  
    })
})
.then(null, function(err) {
  console.error("An error was thrown! By Promise... " + err);
});
driver.quit();

要执行点击,我们可以使用其中任何一项,在IE

上进行测试
element.click(); 
driver.actions().click(element).perform();
driver.executeScript("var tmp = arguments[0]; tmp.click()", element);