Tuesday, November 20, 2012

Selenium Webdriver Basics

Some basic Selenium WebDriver stuff:

1. Click a RadioButton:
var radioButton = driver.FindElements(By.Name("colorname"))[0];
radioButton.Click();

2. Get a RadioButton value:

var radioButtons = driver.FindElements(By.Name("colorname"));
foreach (var radioButton in radioButtons)
{
  if (radioButton.Selected)
    Console.WriteLine(radioButton.GetAttribute("value"));
}

3. Check Checkbox:
var checkBox = driver.FindElement(By.Id("checkboxId"));
checkBox.Click();

4. Select from dropdown list:
var select = driver.FindElement(By.Id("elementId"));
var option = select.FindElemets(By.TagName("optionTagName"))[0];
option.Click();

5. Get text row from table:
var table = driver.FindElement(By.TagName("table"));
var row = table.FindElements(By.TagName("td"))[0];
Console.WriteLine(row.Text);

6. Get element using XPath:
var element = driver.FindElement(By.XPath("//*[@id="gbqfq"]"));


7. Implicit waiting:
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));


8. Explicit waiting:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var element = wait.Until(d=>
              {
                   return driver.FindElements(By.ClassName("class"))[0];
               });
element.Click();

No comments:

Post a Comment