/Selenium_Documentation

Page 95

Selenium Documentation, Release 1.0

click

adminHomeForm

Or, in Selenium-RC selenium.click( "adminHomeForm" );

Your application, however, may generate HTML with IDs that are generated dynamically and therefore the ID itself varies on different instances of the webpage under test. For instance, HTML for a dynamic page element might look like this. <input type= "checkbox" value= "true" id= "addForm:_ID74:_ID75:0:_ID79:0:checkBox" name= "addForm:_ID74:_ID75:0:_ID79:0:checkBox" />

This defines a checkbox. Its ID and name attributes (both addForm:_ID74:_ID75:0:_ID79:0:checkBox) are dynamically generated values. In this case, using a standard locator would look something like the following. click

addForm:_ID74:_ID75:0:_ID79:0:checkBox

Or, again in Selelenium-RC selenium.click("addForm:_ID74:_ID75:0:_ID79:0:checkBox);

Given the dynamically generated Identifier, this approach would not work. The next time this page is loaded the Identifier will be a different value from the one used in the Selenium command and therefore, will not be found. The click operation will fail with an “element not found� error. To correct this, a simple solution would be to just use an XPath locator rather than trying to use an ID locator. So, for the checkbox you can simply use click

//input

Or, if it is not the first input element on the page (which it likely is not) try a more detailed Xpath statement. click

//input[3]

Or click

//div/p[2]/input[3]

If however, you do need to use the ID to locate the element, a programmed solution is required. Another solution is to capture this ID from the website itself, before you need to use it in a Selenium command. It can be done like this. String[] checkboxids = selenium.getAllFields(); // Collect all input IDs on page. if(!GenericValidator.IsBlankOrNull(checkboxids[i])) // If collected ID is not null. { // If the ID starts with addForm if(checkboxids[i].indexOf( "addForm" ) > -1) { selenium.click(checkboxids[i]);

7.4. Choosing a Location Strategy

89


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.