msbyuva
msbyuva

Reputation: 3615

OpenQA.Selenium.NoSuchElementException was unhandled + C# + Another Website

I am new to selenium, currently exploring on how it works. Started using it for ASP.NET application, i am using C# Selenium driver, IE Driver server ( 32 bit as it's faster than 64 bit)

I navigated to a application there I am clicking a link which should take me to ANOTHER WEBSITE where I have to find a textbox and clear it and enter some text (SendKeys) and then click a button.

When it goes to a another website from main website, It's unable to find the element ( I tried using by.ID and by.Name). I made sure element is available on the webpage. As recommened I used ImplicitlyWait but no luck, tried thread.sleep() no lucK.. Does the test needs to be on the same website which launched initially ?.. Below is my code snippet.. Please help me..

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("[email protected]");


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

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

Upvotes: 4

Views: 2231

Answers (1)

Saifur
Saifur

Reputation: 16201

You need to switchTo newly opened window and set focus to it in order to send any commands to it

string currentHandle = driver.CurrentWindowHandle;

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

After you done with newly opened window do(as need)

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

More perfectly use PopupWindowFinder class

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("[email protected]");


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

Upvotes: 3

Related Questions