Skip to content

Instantly share code, notes, and snippets.

@MESWEB
Created May 1, 2020 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MESWEB/975e1f8329372bc67713cf0535a8ac41 to your computer and use it in GitHub Desktop.
Save MESWEB/975e1f8329372bc67713cf0535a8ac41 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Opera;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using NUnit.Framework;
namespace SeleniumDemo
{
class Program
{
static void Main(string[] args)
{
/* Declaring variables for web drivers */
IWebDriver driver;
WebDriverWait wait;
using (driver = new FirefoxDriver())
{
/* Assigning timeout */
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
/* Navigating to google.co.in */
driver.Url = "https://google.com";
wait.Until<bool>((d) => { return d.Title.Contains("google"); });
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Navigate().GoToUrl("https://msn.com");
wait.Until<bool>((d) => { return d.Title.Contains("MSN"); });
IList<string> existingHandles = driver.WindowHandles;
driver.SwitchTo().Window(driver.WindowHandles.First());
//elementThatPopsUpNewTab.Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(120)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[text()='Privacy']")));
IWebElement registerLink = driver.FindElement(By.XPath("//*[text()='Privacy']"));
registerLink.Click();
WebDriverWait wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
string newTabHandle = wait1.Until<string>((d) =>
{
string foundHandle = null;
IList<string> differentHandles = d.WindowHandles.Except(existingHandles).ToList();
if (differentHandles.Count > 0)
{
foundHandle = differentHandles[0];
}
Console.WriteLine(foundHandle);
return foundHandle;
});
driver.SwitchTo().Window(newTabHandle);
foreach (var item in driver.FindElements(By.TagName("a")))
{
Console.WriteLine(item.GetAttribute("href"));
}
Console.Read();
Thread.Sleep(9120000);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment