使用C#的Selenium WebDriver:从下拉菜单中选择项目(不在IE中工作)

时间:2015-11-17 12:19:08

标签: c# selenium

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

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>

我的C#代码仅适用于FireFox:

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中运行?

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我使用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();

答案 2 :(得分:0)

这对我有用:

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