【问题标题】:Selenium WebDriver with C#: select item from drop-down menu (doesn't work in IE)带有 C# 的 Selenium WebDriver:从下拉菜单中选择项目(在 IE 中不起作用)
【发布时间】:2016-02-17 02:59:11
【问题描述】:

需要从默认隐藏的下拉菜单中选择第二项。问题是它在 Fire Fox 41 浏览器中运行良好,但在 Internet Explorer 11 中运行良好。我在 Visual Studio 2010 中使用带有 C# 和 nUnit 的 Selenium Web 驱动程序。使用 Selenium Server 和 IEDriver 在远程 VM 上执行测试。

HTML 看起来像:

<ul id="CVC" class="buttonMenu" style="visibility: hidden; left: 183px;">
  <li class="menuItem">First</li>
  <li class="menuItem">Second</li>
  <li class="menuItem">Third</li>
</ul>

我有只能在 FireFox 中使用的 C# 代码:

var menu = wd.FindElement(By.Id("CVC"));
var menuLi = menu.FindElements(By.TagName("li"));
menuLi[1].Click();
wd.FindElement(By.Id("TITLE")).SendKeys("blabla"); //continue to work with appeared pop-up 
wd.FindElement(By.Id("CVC_OK")).Click();

当我在 Internet Explorer 中运行测试时出现错误:

Test Name:  Bookmark
Test FullName:  EEE.Tests.BT.BB
Test Source:    d:\Selenium\Automation\EEEAutomation\EEEAutomation\Tests\BT.cs : line 19
Test Outcome:   Failed
Test Duration:  0:00:39.319

Result Message: OpenQA.Selenium.ElementNotVisibleException : Cannot click on element (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 35 milliseconds
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'
System info: host: 'wkqacl0801', ip: '10.101.6.104', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_60'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, pageLoadStrategy=normal, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:39901/, takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=dismiss}]
Session ID: 6f09c88a-bd73-4cab-9312-0587c8345023
Result StackTrace:  
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at EEE.Tests.SubTests.CreateBBSubTest.Execute(IWebDriver wd) in d:\Selenium\Automation\EEEAutomation\EEEAutomation\Tests\SubTests\CreateBBSubTest.cs:line 103
at EEE.Tests.BT.BB() in d:\Selenium\Automation\EEEAutomation\EEEAutomation\Tests\BT.cs:line 54

有谁知道如何让它在 Internet Explorer 11 中运行?

【问题讨论】:

    标签: c# selenium


    【解决方案1】:

    不确定是否应将 IE 视为真正的浏览器。无论如何,我重读了您的问题,您似乎希望 IE 在单击事件上像浏览器一样工作。在这种情况下,您可能想帮助它:

    using (var wd = new InternetExplorerDriver(
      new InternetExplorerOptions {EnableNativeEvents = false}))
    {
      //your code
    }
    

    【讨论】:

    • 早些时候,当我尝试使用 SelectElement 时,我看到了一个错误: OpenQA.Selenium.Support.UI.UnexpectedTagNameException : Element 应该被选中,但是是 ul 所以,看起来它不适用于
    • 当我使用“var result”时,VS 显示错误:“无法将 void 分配给隐式类型的局部变量”所以我没有使用它:wd.FindElement(By.Id("CVC" )).FindElement(By.CssSelector("option[value='1']")).Click();但收到一个错误:“OpenQA.Selenium.NoSuchElementException : Unable to find element with css selector == option[value=''1"]”
    • 正如预期的那样, wd.FindElement(By.Id("CVC")).FindElements(By.TagName("li"))[1].Select();在火狐浏览器中运行良好。切换到 IE 时收到错误:“OpenQA.Selenium.ElementNonVisibleException: 无法点击元素”。
    • 抱歉回复晚了。当我使用本机事件 = false 时,错误仍然存​​在。一个例子: var desiredCapabilities = DesiredCapabilities.InternetExplorer(); desiredCapabilities.SetCapability("nativeEvents", false); return new RemoteWebDriver(new Uri(Settings.Default.RemoteUrl), desiredCapabilities);但毕竟我找到了解决办法。
    【解决方案2】:

    我通过使用 JavaScript 找到了解决方案。我不确定这是不是最好的解决方案,但至少它不仅适用于 FF,而且适用于 IE 浏览器:

    ((IJavaScriptExecutor)wd).ExecuteScript("$('#CVC li:eq(1)').click()");
    wd.FindElement(By.Id("TITLE")).Clear();
    wd.FindElement(By.Id("TITLE")).SendKeys("blabla");
    wd.FindElement(By.Id("CVC_OK")).Click();
    

    【讨论】:

      【解决方案3】:

      这对我有用:

      _driver.FindElement(By.Id("IdOfControl")).SendKeys(value);
      

      【讨论】:

        猜你喜欢
        • 2016-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-09
        • 1970-01-01
        • 2014-02-26
        相关资源
        最近更新 更多