1
<div id="ctl00_ContentHolder_vs_ValidationSummary" class="errorblock">
   <p><strong>The following errors were found:</strong></p>
   <ul><input type="hidden" Name="SummaryErrorCmsIds" Value="E024|E012|E014" />
   <li>Please select a title.</li>
   <li>Please key in your first name.</li>
   <li>Please key in your last name.</li>
   </ul>
</div>

here is my snippet for example. i want to get the value of ID i.e., ct100_contentHolder_vs_ValidationSummary. using selenium web driver. h

4

3 回答 3

4

You can try this :

String id=driver.findElementByXpath("//div[@class='errorblock']").getAttribute("id"));

But in this case the class of this division should be unique.

于 2012-11-22T05:46:22.170 回答
0

Use following code to extract id of first div:

WebElement div = driver.findElement(By.tagName("div"));
div.getAttribute("id");

This is the code for all div available on the page:

List<WebElement> div = driver.findElements(By.tagName("div"));
for ( WebElement e : div ) {    
    div.getAttribute("id");    
}
于 2014-04-23T12:40:54.577 回答
0

I know this answer is really late but I wanted to put this here for those who come later. Searching by XPath should be avoided unless absolutely necessary because it is more complicated, more error prone, and slower. In this case you can easily do what the accepted answer did without having to use XPaths:

String id = driver.findElement(By.cssSelector("div.errorblock")).getAttribute("id");

Some explanation... this line finds the first element (.findElement vs .findElements) using a CSS Selector. The CSS Selector, div.errorblock, locates all div elements with the class (symbolized by the period .) errorblock. Once it is located, we get the ID using .getAttribute().

CSS Selectors are a great tool that all automators should have in their toolbox. There's a great CSS Selector reference here: http://www.w3.org/TR/selectors/#selectors.

于 2015-08-10T14:22:12.303 回答