Reputation: 413
Recently I started learning WebDriver as my client that I am working for is planning to use WebDriver for automating web applications.
I have doubts regarding how WebDriver locates the elements on webpage whose id's are dynamically changing (like changing for every login to application). Can anyone explain how we can accomplish this task with WebDriver?
Upvotes: 2
Views: 6339
Reputation: 181
You can try: https://github.com/sdl/Testy/ syntax is preaty simple:
// 1. import maven dependency
// 2. init Framework in your TestBase after initializing your driver
WebDriverConfig.init(driver);
// 3. any actions based on many many attributes
WebLocator logoutBtn = new WebLocator().setText("Log Out");
// make sure the element is rendered even after fiew seconds (5 by default)
logoutBtn.assertReady();
// or do any actions with elements
logoutBtn.click();
// more properties for selecting/testing specific element with wanted attributes
WebLocator closeIcon = new WebLocator().setClasses("close").setTitle("Close");
WebLocator minimIcon = new WebLocator().setClasses("minimize").setTitle("Minimize");
Upvotes: 0
Reputation: 77
You can achieve it by using contains()
method which is matched element name.
WebElement cls = (new WebDriverWait(ff, 10))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='_54nc']/span/
span[contains(text(), 'Log Out')]")));
Upvotes: 0
Reputation: 1342
We had the same problem and we ended up using jquery selectors, especially if jquery is already available at your client-side. In the ZK framework that we use, we already have some jquery extensions so we could simply write:
assertEquals(driver.findElement(By.jq("@label:eq(0)")).getText(),"ROOT_MESSAGE");
where as the By.jq() effectively boils down to :
return (WebElement)((JavascriptExecutor)context).executeScript("return jq('" + selector + "').get(0);");
Upvotes: 2
Reputation: 53
Locating elements with dynamic id's can be fragile. I would rather use some visible text with for example xpath expression. My point is that in most cases the visible text is usually part of the requirements or specification of the application and id's are not. Therefore the id's are more likely to change and visible text not so.
For example to locate the username field in login form I might use xpath:
//label[.='Username']//following::input[1]
This is assuming there is a label "Username" before the input field.
I have found Firebug console function $x("xpath string") to be very useful when debugging those xpaths.
Upvotes: 5
Reputation: 585
For those elements on the webpage whose ids are dynamically changing:
You can try locating the elements by their Xpath locator or CSS locator
You can find more information about the locator strategies that can be employed while using WebDriver here . Have a look at these and you would figure out the various locator strategies.
In order to understand the concept for locating dynamic elements you can have a look at the Selenium1 documents here. However pls note the api in this link is for Selenium 1. But you can use the concept and the locator strategy/api provided for the WebDriver earlier to accomplish your task
Upvotes: 3