首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C# Selenium -IE11-单击链接将打开新窗口而不是新选项卡。

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

Stack Overflow用户
提问于 2017-11-08 16:20:27
回答 1查看 2K关注 0票数 1

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

代码语言:javascript
运行
复制
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秒钟内执行,所以我不认为这就是问题所在。

有人有什么想法吗?

EN

Stack Overflow用户

发布于 2017-11-10 20:30:51

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

我的建议是,无论您的语言是C#还是其他语言,都要不惜一切代价避免使用。即使是IE 11,我想做的事情都没有用。测试使用Chrome作为您的首选。也许它将为您节省三个工作日的故障排除和调试。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47184652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档