Click a button in an iframe with Selenium. The key is to use 'driver.SwitchTo().Frame()' and 'driver.SwitchTo().DefaultContent()'.
// check if the iframe is present var cookieConsentIframe = WebDriverExtensions.TryFindElement(By.Id("qb_cookie_consent_main")); if(cookieConsentIframe != null) { // find a specific element in the iframe and click on it driver.SwitchTo().Frame(cookieConsentIframe); var consent = WebDriverExtensions.TryFindElement(By.Id("buttonAccept")); if(consent != null) consent.Click(); // swithc back to the default webpage driver.SwitchTo().DefaultContent(); }
Ofcourse you can do all kind of actions in the iframe. I just needed to click a specific button element. Don't forget to switch back to the default page afterwards.
Find an img element which had a specific jpg in the src field. The CssSelector provides the solution.
driver.FindElement(By.CssSelector("img[src*='test.jpg']"));
The original path is: http://mydomain/content/images/test.jpg
Find an element which contains a part of an specific Id.
var e = driver.FindElement(By.CssSelector("div[id*='dialogFormat']")); // original id = 'dialogFormat01' var e = driver.FindElement(By.CssSelector("li[id*='design']")); // original id = 'design05'
Multiple conditions with CssSelector:
var test = driver.FindElements(By.CssSelector("div[id^='i'][id*='_']"));
Finds all elements where id starts with an 'i' and id contains an '_'
How to take a screenshot with Selenium.
public static void TakeScreenshot(int number) { // check and/or create the Screenshots directory var screenshotDir = Directory.GetCurrentDirectory() + @"\Screenshots"; if (!Directory.Exists(screenshotDir)) Directory.CreateDirectory(screenshotDir); var failScreen = ((ITakesScreenshot)Cache.Driver).GetScreenshot(); var fileName = String.Format("screenshot{0}.png", number); failScreen.SaveAsFile(screenshotDir + "/" + fileName, ImageFormat.Png); }
I use it when a 'NoSuchElementException' occurs. Makes it easier to find where the test fails.
catch (NoSuchElementException) { // take a screenshot General.TakeScreenshot(ct); Console.WriteLine(); Console.WriteLine(string.Format("NoSuchElementException occured!{0}Product: {1} - {2} - {3} - {4}", Environment.NewLine, ct.Category, ct.CardType, ct.CardSize, ct.Design)); continue; }
Set or select a value in a combobox with Selenium and C#.
var exMonth = new SelectElement(driver.FindElement(By.Id("ExpDate_Month"))); // the combobox exMonth.DeselectAll(); // only valid when the SELECT support multiple selections exMonth.SelectByText("06"); // set value by text
Some extension methods I use for the Selenium Webdriver.
using System; using System.Collections.ObjectModel; using OpenQA.Selenium; using OpenQA.Selenium.Support.UI; namespace SeleniumTest.Utils { public static class WebDriverExtensions { private static bool _acceptNextAlert = true; /// <summary> /// Find element with wait /// </summary> /// <param name="driver">the webdriver</param> /// <param name="by">search condition</param> /// <param name="timeoutInSeconds">search time out</param> /// <returns>the found webelement or null</returns> public static IWebElement FindElementWithWait(this IWebDriver driver, By by, int timeoutInSeconds) { try { if (timeoutInSeconds > 0) { var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds)); return wait.Until(drv => drv.FindElement(@by)); } return driver.FindElement(@by); } catch (NoSuchElementException) { return null; } } /// <summary> /// Find elements with wait /// </summary> /// <param name="driver">the webdriver</param> /// <param name="by">search condition</param> /// <param name="timeoutInSeconds">search time out</param> /// <returns>the found webelements or null</returns> public static ReadOnlyCollection<IWebElement> FindElementsWithWait(this IWebDriver driver, By by, int timeoutInSeconds) { if (timeoutInSeconds > 0) { var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds)); return wait.Until(drv => (drv.FindElements(@by).Count > 0) ? drv.FindElements(@by) : null); } return driver.FindElements(@by); } /// <summary> /// Method to find an element without throwing an error /// </summary> /// <param name="by">search conditions</param> /// <param name="element">the webelement returned</param> /// <returns></returns> public static IWebElement TryFindElement(By by) { IWebElement element; try { if (Cache.Driver != null) { IWebDriver driver = Cache.Driver; return element = driver.FindElement(by); } throw new NullReferenceException("The webdriver is not set!"); } catch (NoSuchElementException) { return null; } } /// <summary> /// Checks if an element is present without throwing an error /// </summary> /// <param name="by">search condition</param> /// <returns>bool</returns> public static bool IsElementPresent(By by) { try { if (Cache.Driver != null) { IWebDriver driver = Cache.Driver; driver.FindElement(by); return true; } throw new NullReferenceException("The webdriver is not set!"); } catch (NoSuchElementException) { return false; } } /// <summary> /// checks if an element is visible /// </summary> /// <param name="element">the webelement</param> /// <returns>bool</returns> public static bool IsElementVisible(IWebElement element) { return element.Displayed && element.Enabled; } /// <summary> /// checks if there is an Alert present /// </summary> /// <returns>bool</returns> public static bool IsAlertPresent() { try { if (Cache.Driver != null) { IWebDriver driver = Cache.Driver; driver.SwitchTo().Alert(); return true; } throw new NullReferenceException("The webdriver is not set!"); } catch (NoAlertPresentException) { return false; } } /// <summary> /// Close the Alert and get its text /// </summary> /// <returns>returns the Alert text</returns> public static string CloseAlertAndGetItsText() { try { if (Cache.Driver != null) { IWebDriver driver = Cache.Driver; IAlert alert = driver.SwitchTo().Alert(); string alertText = alert.Text; if (_acceptNextAlert) alert.Accept(); else alert.Dismiss(); return alertText; } throw new NullReferenceException("The webdriver is not set!"); } finally { _acceptNextAlert = true; } } } }