![](/img/trans.png)
[英]C# IE11 selenium webdriver, No new window handles with new tab
[英]C# Selenium - IE 11 - Clicking link opens new window instead of new tab
我正在C#中運行Selenium測試以打開URL,使用提供的用戶名和密碼登錄,然后導航到包含可下載報告的頁面。 請參閱下面的代碼(注意:保留網站名稱和用戶名/密碼):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Support.UI;
namespace SeleniumProject
{
class SeleniumTest
{
public static void Main(string[] args)
{
#region Constants
//User Account Information
const string username = "MyUsername";
const string password = "MyPassword";
//Initial Login Page URL and Elements
const string urlLogin = "MyURL";
const string usernameLoginName = "username";
const string passwordLoginName = "password";
const string submitLoginClassName = "btnAlign";
//Welcome Page Element
const string profileWelcomeClassName = "mstrLargeIconViewItemLink";
#endregion
int elementListIndex = 0;
IWebDriver driver = new InternetExplorerDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl(urlLogin);
driver.FindElement(By.Name(usernameLoginName)).SendKeys(username);
driver.FindElement(By.Name(passwordLoginName)).SendKeys(password);
driver.FindElement(By.ClassName(submitLoginClassName)).Click();
if (driver.Title == "Servicer Performance Profile Home. MicroStrategy")
{
try
{
driver.FindElement(By.ClassName(profileWelcomeClassName)).Click();
}
catch (NoSuchElementException ex)
{
//failed
}
}
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//img[contains(@src,'images/freddiemac/sppdash/navigation-drawer-1.png')]")));
IReadOnlyList<IWebElement> elementList = driver.FindElements(By.XPath("//img[contains(@src,'images/freddiemac/sppdash/navigation-drawer-1.png')]"));
string mainHandle = driver.CurrentWindowHandle;
foreach (var element in elementList)
{
if (element.Displayed && elementListIndex == 5)
{
element.Click();
driver.FindElement(By.XPath("//div[contains(.,'EDR Overview')]")).Click();
break;
}
else
{
elementListIndex++;
}
}
}
}
}
發生的事情是,每當我執行在foreach循環內嵌套的if語句內的最后一個Click()事件時,而不是鏈接的正常行為,即在同一IE中打開新標簽頁時,它都將作為新窗口打開並返回到上一頁。 通常,每當我手動登錄該網站並單擊此鏈接時,都會打開一個新選項卡,其中包含另一個下載鏈接。 那就是我要去的頁面。
我不知道為什么這個新的瀏覽器窗口打開的是先前的頁面,而不是我請求的目標頁面。 這與Selenium和IE11不能相處有關嗎? 另一個想法是當前的登錄會話以某種方式到期,但是這一切都在不到10秒的時間內執行,因此我不認為這是問題所在。
有人有什么想法嗎?
此問題已得到解決。 經過許多次嘗試更改IE設置以處理新標簽,以編程方式執行JavaScript onclick()事件(而不是使用本機瀏覽器單擊命令),打開和切換空白標簽,嘗試右鍵單擊鍵盤命令等失敗,問題歸結為IE根本不符合我的意圖。 首次嘗試使用Google Chrome瀏覽器被證明是成功的。 網站運行正常,應該觸發新標簽的鏈接確實觸發了新標簽。
我對Selenium Webdriver測試新手的建議,無論您的語言是C#還是其他語言,都是不惜一切代價避免使用Internet Explorer。 即使使用IE 11,我也不想做任何事情。 首選使用Chrome進行測試。 也許它將為您節省三個工作日的故障排除和調試時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.