Reputation: 97
I'm currently having troubles on locating this element with dynamic id. Here are the screenshots below.
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
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
Reputation: 3004
try this:
String varText = driver.findElement(By.cssSelector("div.triggerFirst>div:nth-child(1)>a")).getText();
Upvotes: 0
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
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