Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.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
OpenQA.Selenium.NoTouchElementException未处理+;C#和#x2B;另一个网站_C#_Asp.net_Selenium_Selenium Webdriver - Fatal编程技术网

OpenQA.Selenium.NoTouchElementException未处理+;C#和#x2B;另一个网站

OpenQA.Selenium.NoTouchElementException未处理+;C#和#x2B;另一个网站,c#,asp.net,selenium,selenium-webdriver,C#,Asp.net,Selenium,Selenium Webdriver,我是硒的新手,目前正在探索硒的工作原理。开始在ASP.NET应用程序中使用它,我使用的是C#Selenium驱动程序,即驱动程序服务器(32位,因为它比64位快) 我导航到了一个应用程序,我点击了一个链接,这个链接应该会把我带到另一个网站,在那里我必须找到一个文本框,清除它,输入一些文本(SendKeys),然后点击一个按钮 当它从主网站转到另一个网站时,它无法找到元素(我尝试使用by.ID和by.Name)。我确保该元素在网页上可用。按照建议,我使用了隐式wait但没有运气,尝试了thread

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

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

当它从主网站转到另一个网站时,它无法找到元素(我尝试使用by.ID和by.Name)。我确保该元素在网页上可用。按照建议,我使用了隐式wait但没有运气,尝试了thread.sleep()没有运气。。测试是否需要在最初启动的同一网站上进行?。。下面是我的代码片段。。请帮帮我

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();
        }
    }
}

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

string currentHandle = driver.CurrentWindowHandle;

driver.SwitchTo().Window(driver.WindowHandles.ToList().Last());
完成新打开的窗口后(根据需要)

更完美地使用类


谢谢你的快速回复。。它可以工作,但当前在应用程序中单击EMailLink时,另一个窗口会在新窗口中打开(如taregt=_blank),但这是作为单独窗口打开的。也在执行driver.Close()之后;driver.SwitchTo().Window(popupWindowHandle);新窗口已关闭,但在“driver.SwitchTo().window(PopupUpindowHandle)”;“NoSuchWindowException was unhandle”处引发错误。未找到任何窗口。您是说在单击后打开两个窗口吗?单击后否新窗口作为弹出窗口打开-根据单击时的应用程序,它在另一个选项卡中打开,但不是作为新窗口打开。。。。说到错误。。。它工作得很好。。这是我的错误…是的,可能是我对这东西还不熟悉真的不知道。。但是你的帖子帮了我。。。谢谢嗯,这个窗口句柄有点混乱。你还面临其他问题吗?如果是的话,我会尽力帮忙
driver.Close();
driver.SwitchTo().Window(currentHandle );
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);