Webdriver – Drop Down boxes, Check Boxes & Filling in forms

Here is how you can select values in a drop down box: The drop Down has an ID called “Domain” and values Yahoo, Virgin, AOL.

SelectElement select = new SelectElement(_webdriver.FindElement(By.Id(“Domain”)));

Select.DeselectAll();

select.SelectByText(“Yahoo”);

This will deselect all options, and then select the option withthe displayed text of “Yahoo”.

You can select Radio Boxes and Check Boxes by the following command:

driver.FindElement(By.Id(“SingleJourneyCheckBox”)).Click();

You can find out whether this checkbox/radiobox is alrady selected by this command.

IWebElement checkbox = driver.FindElement(By.Id(“SingleJourneyCheckBox”));

if(checkbox.Selected)

{

//do something here.

}

If you were filling out a form and  once you’ve finished, you probably want to submit it. One way to do this would be to find the “submit” button and click it:

driver.findElement(By.id(“submit”)).Click(); // Assume the button has the ID “submit”

Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will crawl up the DOM until it finds the enclosing form and then calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be thrown:

element.submit();

Leave a Reply

Your email address will not be published.