Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Selenium-IE 11-单击链接将打开新窗口而不是新选项卡_C#_Selenium_Selenium Webdriver_Internet Explorer 11 - Fatal编程技术网

C#Selenium-IE 11-单击链接将打开新窗口而不是新选项卡

C#Selenium-IE 11-单击链接将打开新窗口而不是新选项卡,c#,selenium,selenium-webdriver,internet-explorer-11,C#,Selenium,Selenium Webdriver,Internet Explorer 11,我正在C#中运行Selenium测试,以打开URL,使用提供的用户名和密码登录,然后导航到包含可下载报告的页面。请参见下面的我的代码(注意:网站名称和用户名/密码被保留): 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用OpenQA.Selenium; 使用OpenQA.Selenium.IE; 使用OpenQA.Selenium.Support; 使用OpenQA.S

我正在C#中运行Selenium测试,以打开URL,使用提供的用户名和密码登录,然后导航到包含可下载报告的页面。请参见下面的我的代码(注意:网站名称和用户名/密码被保留):

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用OpenQA.Selenium;
使用OpenQA.Selenium.IE;
使用OpenQA.Selenium.Support;
使用OpenQA.Selenium.Support.UI;
名称空间Selenium项目
{
硒类测试
{
公共静态void Main(字符串[]args)
{
#区域常数
//用户帐户信息
常量字符串username=“MyUsername”;
const string password=“MyPassword”;
//初始登录页面URL和元素
常量字符串urlogin=“MyURL”;
常量字符串usernameLoginName=“username”;
const string passwordLoginName=“password”;
常量字符串submitLoginClassName=“btnAlign”;
//欢迎页面元素
常量字符串profileWelcomeClassName=“mstrLargeIconViewItemLink”;
#端区
int elementListIndex=0;
IWebDriver=newInternetExplorerDriver();
driver.Manage().Window.Maximize();
driver.Navigate().gotour(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”)
{
尝试
{
driver.FindElement(By.ClassName(profileWelcomeClassName)).Click();
}
捕获(无接触元素例外)
{
//失败
}
}
WebDriverWait wait=新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(15));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath(“//img[contains(@src,'images/freddiemac/sppdash/navigation-drawer-1.png'))));
IReadOnlyList elementList=driver.FindElements(By.XPath(//img[contains(@src,'images/freddiemac/sppdash/navigation-drawer-1.png'))));
字符串mainHandle=driver.CurrentWindowHandle;
foreach(elementList中的var元素)
{
if(element.displated&&elementListIndex==5)
{
元素。单击();
FindElement(By.XPath(“//div[contains(,'EDR Overview')]”),单击();
打破
}
其他的
{
elementListIndex++;
}
}
}
}
}
当我执行嵌套在foreach循环中的if语句中的last Click()事件时,所发生的事情就不同于链接在同一IE中打开新选项卡的正常行为,而是作为一个新窗口打开并返回到上一页。通常,每当我手动登录此网站并单击此链接时,会打开一个新选项卡,其中包含另一个下载链接;这就是我想进入的页面

我不知道为什么这个新的浏览器窗口打开的是以前的页面,而不是我请求的目标页面。这可能与Selenium和IE11不合拍有关吗?另一个想法是当前登录会话不知何故会过期,但这一切都是在不到10秒内执行的,所以我不认为这是问题所在


有人有什么想法吗?

这个问题已经解决了。在多次尝试更改IE设置以处理新选项卡、以编程方式执行JavaScript onclick()事件(而不是使用本机浏览器单击命令)、打开空选项卡并在空选项卡之间切换、尝试右键单击键盘命令等失败后。,问题归根结底是IE与我试图做的事情不兼容。谷歌浏览器的第一次尝试被证明是成功的。该网站表现正常,本来应该触发新标签的链接确实触发了新标签


对于那些刚刚接触Selenium webdriver测试的人,我的建议是,无论您的语言是C#还是其他语言,都要不惜一切代价避免使用Internet Explorer。即使有了IE 11,我想做的事情也没有一个奏效。使用Chrome作为第一选择进行测试。也许这将为您节省三个工作日的故障排除和调试时间。

此问题已得到解决。在多次尝试更改IE设置以处理新选项卡、以编程方式执行JavaScript onclick()事件(而不是使用本机浏览器单击命令)、打开空选项卡并在空选项卡之间切换、尝试右键单击键盘命令等失败后。,问题归根结底是IE与我试图做的事情不兼容。谷歌浏览器的第一次尝试被证明是成功的。该网站表现正常,本来应该触发新标签的链接确实触发了新标签


对于那些刚刚接触Selenium webdriver测试的人,我的建议是,无论您的语言是C#还是其他语言,都要不惜一切代价避免使用Internet Explorer。即使有了IE 11,我想做的事情也没有一个奏效。使用Chrome作为第一选择进行测试。也许这将为您节省三个工作日的故障排除和调试时间。

可能重复:尝试按照您提供的链接中的建议更改IE的设置。我的问题还在继续。未解决。为什么单击两次?元素。单击();和driver.FindElement(By.XPath(“//div[contains(,'EDR Overview')]”);嗨,Koen。我的foreach循环将单击elementList中索引5处的元素。单击后,将显示一个文本为“EDR概述”的div
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++;
                }
            }
        }
    }
}