簡體   English   中英

使用C#的Selenium WebDriver:從下拉菜單中選擇項(在IE中不起作用)

[英]Selenium WebDriver with C#: select item from drop-down menu (doesn't work in IE)

需要從默認隱藏的下拉菜單中選擇第二項。 問題是,它在Fire Fox 41瀏覽器中工作正常,但在Internet Explorer 11中卻無法正常工作。我在SStudio和C#中使用了Selenium Web Driver,在Visual Studio 2010中使用了nUnit。在具有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中工作?

不知道IE是否應該被視為真正的瀏覽器。 無論如何,我都會重讀您的問題,看來您希望IE像單擊事件中的瀏覽器一樣工作。 在這種情況下,您可能需要幫助:

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

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

這對我有用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM