Skip to content

Instantly share code, notes, and snippets.

@nadvolod
Created May 15, 2016 02:09
Show Gist options
  • Save nadvolod/b930ed9e38bf437c708f24171994ade1 to your computer and use it in GitHub Desktop.
Save nadvolod/b930ed9e38bf437c708f24171994ade1 to your computer and use it in GitHub Desktop.
How to handle a complicated with application with frames and dynamic elements
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var desiredString = "Courtland Street Northeast, Atlanta, GA, United States";
var driver = new FirefoxDriver();
driver.Url = "https://booking.supershuttle.com/?RezMode=0&A=&GC=&direction=T&port=ATL&WC=0&Lng=en-US";
driver.Navigate().GoToUrl(driver.Url);
//Default timeout for element
try
{
driver.FindElementById("airport-pickup-address").SendKeys("Hilton Atlanta");
}
catch (UnhandledAlertException e)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.Id("ve-chat-iframe")));
var closeButton = wait.Until(ExpectedConditions.ElementIsVisible(By.ClassName("close-button")));
if(closeButton != null)
closeButton.Click();
driver.SwitchTo().DefaultContent();
driver.FindElementById("airport-pickup-address").SendKeys("Hilton Atlanta");
var elements2 = driver.FindElementsByXPath("//*[@class='pac-item']/span[3]");
foreach (var option in elements2)
{
if (!option.Text.Contains(desiredString)) continue;
option.Click();
return;
}
}
finally
{
driver.Close();
driver.Quit();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment