user3713453
user3713453

Reputation: 97

Selenium WebDriver Java locating an element with dynamic ID

I'm currently having troubles on locating this element with dynamic id. Here are the screenshots below.

enter image description here

enter image description here

What i have right now is the element of Variables (8) //a[contains(.,'Variables (8)')]. What i need is only the "Variables" since the number 8 is always changing.

Any thoughts on this? Any ideas will be much appreciated. Thanks

Upvotes: 0

Views: 1204

Answers (4)

Nitesh
Nitesh

Reputation: 187

What I understand from your question is you want to locate the <a> tag which contains text "Variables". Try using this xpath:

//div[@class="field"]/a[contains(.,"Variables")]

This xpath will locate the <a> tag after the div tag with class name =field and Contains method with <a> tag will find the element which contains text "Variables"

Upvotes: 1

noor
noor

Reputation: 3004

try this:

 String varText =    driver.findElement(By.cssSelector("div.triggerFirst>div:nth-child(1)>a")).getText();

Upvotes: 0

Nitin Subramanya
Nitin Subramanya

Reputation: 41

You can try the below:

driver.findElement(By.cssSelector("a:contains('Variables')"));

If you want the word "Variables" , use the below:

String str = driver.findElement(By.cssSelector("a:contains('Variables')")).getText().split(" ")[0];

Hope this Helps....

Upvotes: 1

Guy
Guy

Reputation: 50949

First of all 'Variables (8)' is not Id, its text. Id's are not dynamic since they represent unique identifier for the web element. This will look like this (based on your example):

<div class="field" id="fieldId">

As for your question, you can find the element by partial linked text:

driver.findElement(By.partialLinkText("Variables"));

This will give you the a element no meter what the number is.

Upvotes: 2

Related Questions