0

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

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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....

Comments

1

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"

Comments

0

try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.