OpenQA.Selenium.NoSuchElementException未处理+ C#+另一个网站

时间:2015-03-27 19:34:50

标签: c# asp.net selenium selenium-webdriver

我是selenium的新手,目前正在探索它是如何工作的。开始在ASP.NET应用程序中使用它,我正在使用C#Selenium驱动程序,IE驱动程序服务器(32位,因为它比64位快)

我导航到那里的应用程序我点击一个链接,它应该带我到另一个网站,我必须找到一个文本框并清除它并输入一些文本(SendKeys),然后单击一个按钮。

当它从主网站转到另一个网站时,它无法找到该元素(我尝试使用by.ID和by.Name)。我确保元素在网页上可用。正如推荐我使用ImplicitlyWait但没有运气,尝试过thread.sleep()没有lucK ..测试是否需要在最初推出的同一个网站上?..下面是我的代码片段..请帮助我..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using System.Threading;

namespace mySelenium
{
    class Program
    {
    private static void Main(string[] args)
    {
        IWebDriver driver = new InternetExplorerDriver(@"C:\Users\msbyuva\Downloads\IEDriverServer_Win32_2.45.0\");
        driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

        driver.Navigate().GoToUrl("http://MyorgName.org/Apps/Sites/2015/login.aspx");

        IWebElement userNameTxtBox = driver.FindElement(By.Id("ContentPlaceHolder1_Login1_UserName"));
        userNameTxtBox.SendKeys("MSBYUVA");

        IWebElement passwordTxtBox = driver.FindElement(By.Id("ContentPlaceHolder1_Login1_Password"));
        passwordTxtBox.SendKeys("1234");

        var myButton = driver.FindElement(By.Id("ContentPlaceHolder1_Login1_LoginButton"));
        myButton.Click();

        var EMailLink = driver.FindElement(By.LinkText("Email Testing Link"));
        EMailLink .Click();

        //Thread.Sleep(10000);

// -- HERE IT IS THROWING ERROR (ANOTHER WEBSITE AFTER CLICKING HYPERLINK)

    var toEmailAddress  = driver.FindElement(By.Name("ctl00$ContentPlaceHolder1$txtTo"));
                toEmailAddress.Clear();
                toEmailAddress.SendKeys("msbyuva@gmail.com");


            var chkEmailAttachment = driver.FindElement(By.Name("ctl00$ContentPlaceHolder1$ChkAttachMent"));
            chkEmailAttachment.Click();

            var sendEmailButton = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_BtnSend"));
            sendEmailButton.Click();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您需要切换到新打开的窗口并将焦点设置为它以向其发送任何命令

string currentHandle = driver.CurrentWindowHandle;

driver.SwitchTo().Window(driver.WindowHandles.ToList().Last());

完成新打开的窗口后(根据需要)

driver.Close();
driver.SwitchTo().Window(currentHandle );

更完美地使用PopupWindowFinder

string currentHandle = driver.CurrentWindowHandle;
PopupWindowFinder popUpWindow = new PopupWindowFinder(driver);
string popupWindowHandle = popUpWindow.Click(EMailLink );
driver.SwitchTo().Window(popupWindowHandle);

//then do the email stuff

 var toEmailAddress  = driver.FindElement(By.Name("ctl00$ContentPlaceHolder1$txtTo"));
                toEmailAddress.Clear();
                toEmailAddress.SendKeys("msbyuva@gmail.com");


            var chkEmailAttachment = driver.FindElement(By.Name("ctl00$ContentPlaceHolder1$ChkAttachMent"));
            chkEmailAttachment.Click();

            var sendEmailButton = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_BtnSend"));
            sendEmailButton.Click();
        }
    }
}

//closing pop up window
driver.Close();
driver.SwitchToWindow(currentHandle);