smriti
smriti

Reputation: 1144

How to handle dynamic ID in HTML

I have to click on the button which has id. But this id is generated dynamically. And find By.className() is not doing anything.

The HTML code for the button:

<td class="x-btn-mc">
    <em class="" unselectable="on">
        <button id="cq-gen372" class=" x-btn-text" type="button">OK</button>
    </em>
</td>

How to select the button and click on it in Java?

Upvotes: 1

Views: 363

Answers (2)

Rohit Ware
Rohit Ware

Reputation: 2002

You can go with below options

  //button[text()='OK']    
  xpath=//button[contains(., 'OK')]
  //button[contains(@class, 'x-btn-text')] 

Upvotes: 1

Petr Janeček
Petr Janeček

Reputation: 38424

By.className() really was bugged in IE and some older Selenium versions. I didn't know it is still the case. Anyway! You can search by a lot of things, not just id:

You can try By.xpath("//button[text()='OK']"); if it is the only (or the first) OK button on page.

For more xpaths, see XPath v1.0 on w3.org and XPath v2.0 on w3.org - only for some new browsers!.

Or you can go with css selectors - The w3 again or wikipedia.

Upvotes: 1

Related Questions