Oni
Oni

Reputation: 45

Selenium: How to Click a Variable Button

<a id="btn_yyyy_69" href="javascript:void(0);"
 onclick="checkOnline(this,'69','UCnHasdadhrz3mTd1A7t5TQ','yyyy');"
 class="gbut_red">Yepsribe</a>

and xpath like //*[@id="btn_yyyy_69"] -- 69,It's constantly changing.

i tried like this ;

WebDriverWait waitc = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
waitc.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='btn_yyyy_")));
driver.FindElement(By.XPath("//*[@id='btn_yyyy_")).Click();

Upvotes: 1

Views: 1007

Answers (3)

L.dev
L.dev

Reputation: 93

I think

//a[text()='Yepsribe']

Is the best way to look for elements with dynamic id. It also prevent you from code changes (ex. all button classes or id have changed to another template).

Upvotes: 0

Andersson
Andersson

Reputation: 52665

If you want to match link by partial @id value, you can try

By.XPath("//a[starts-with(@id, 'btn_yyyy_')]")
By.CssSelector("a[id^='btn_yyyy_']")

Also you can locate link by its text:

By.LinkText("Yepsribe")

Upvotes: 2

Anand
Anand

Reputation: 1939

If this is the only button with an id with btn_yyyy in it, use:

//a[contains(@id, 'btn_yyyy')]

That should work only if this is the only button which has an id with that notation in it.

If there are multiple buttons with partial id btn_yyyy and this is the only butotn with text Yepsribe, try:

//a[contains(@id, 'myclass') and text() = 'Yepsribe']

Upvotes: 0

Related Questions