Sukesh Bolar
Sukesh Bolar

Reputation: 49

Handle elements that have changing ids

I am running the script to automate test cases and found that id's keep on changing. Below is my HTML code Firebug for test drive:

<button class="G0036HC-b-a G0036HC-b-o G0036HC-b-g" id="gwt-uid-470" tabindex="0" aria-labelledby="gwt-uid-470" role="button" type="button"><div class="G0036HC-b-j">Click to continue</div></button>

Inspector:

<button class="G0036HC-b-a G0036HC-b-o G0036HC-b-g" id="gwt-uid-320" tabindex="0" aria-labelledby="gwt-uid-320" role="button" type="button"><div class="G0036HC-b-j">Click to continue</div></button>

Only id's changes. Any Help would be appreciated.

Upvotes: 2

Views: 774

Answers (4)

Praneeth
Praneeth

Reputation: 1

use following... //input[@id=’’]/following::input[1]

You can use preceding in the same way to identify the child node

Upvotes: 0

Archit
Archit

Reputation: 176

Considering the following observations, the below code should work fine.

  • All the buttons on the page are prefixed with gwt-uid- with changing number suffix
  • If the buttons on the page have a unique text then use

By.xpath("//button/div[text()='Click to continue']")

  • If there are multiple buttons with same text then use indexes. eg. two close buttons with same text then use,

By.xpath("(//button/div[text()='Close'])[2]") # for the 2nd occurence, mind that selenium is not zero indexed

Upvotes: 0

Saifur
Saifur

Reputation: 16201

Besides, the answer @alecxe suggested I would also suggest you to try with text based xpath search. I often faced issue with wait so also suggest to use explicit wait this.

// //div[.='Click to continue']/..
By byXpath = By.xpath("//div[.='Click to continue']");

WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byXpath));
//    myDynamicElement.click();

Upvotes: 1

alecxe
alecxe

Reputation: 473853

You can get the element by xpath and check that id attribute starts with gwt-uid-:

//button[starts-with(@id, "gwt-uid-")]

Upvotes: 1

Related Questions