简体   繁体   中英

Store the attribute's value of a WebElement in Selenium Webdriver

Ive got a UL that is dynamically populated and the ID is also dynamically generated everytime you click on the "Action" button; is there a way to read the tag of the

<div class="class">
 <ul id="dynamically generated">
  <li class="li_class">
  <li class="li_class">
  <li class="li_class">
  <li class="li_class">
  <li class="li_class">
 </ul>
</div>

Since you didn't mention any language, I'll go with the JAVA syntax :

// String object 'field_value' is created
String fieldValue;  
// The field value is retrieved by the getAttribute("id") Selenium WebDriver predefined method and assigned to the String object.
fieldValue = _driver.findElement(By.ById("dynamically generated")).getAttribute("id")  

It'll get the value of a the given attribute of the element. Will return the current value, even if this has been modified after the page has been loaded.

I know you already accepted an answer but if some portion of the ID is stable, you can search for partial matches. For example, say you have an element

<span id="ctl00_SomeStableTextGoesHere_12345" ...>

where 12345 is dynamically generated. You could search for the start of the ID using CSS Selectors.

driver.findElement(By.cssSelector("span[id^='ctl00_SomeStableTextGoesHere_']"));

There are other options:

  • ^ begins with
  • $ ends with
  • * contains

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM