0

New to Automation , I am using selenium with JAVA.

I have come across a stumbling block our developers do not assign ID to our elements and JFS assigns it own ID , however the ID's are dynamic and keeps on changing.

Also the xpaths and css selectors also contains the dynamic id's.

I have seem some methods of using contains , starts with , however cannot seems to get it correct , when I inspect my element I get below

<button type="button" class="ui-datepicker-trigger ui-button ui-widget ui- 
state-default ui-corner-all ui-button-icon-only" aria-label="Show Calendar" 
aria-haspopup="true" role="button" aria-disabled="false"><span class="ui- 
button-icon-left ui-icon ui-icon-calendar"></span><span class="ui-button- 
text">ui-button</span></button>

 xpath - //[@id="j_idt67:j_idt69:j_idt70:j_idt100:j_idt101:calendar"]/button
 css selector = #j_idt67\3a j_idt69\3a j_idt70\3a j_idt100\3a j_idt101\3a 
calendar > button

I f anyone could assist and advise on how you would locate the element and insure that it would not change , would be of great assistance , if you require further info , let me know , like I said new to automation

4
  • 1
    Did you tried to search SQA archives for handling dynamic id? We get such question weekly, several are listed in "Related". Please explain how your question is different, or your question will be closed as duplicate. Commented Apr 10, 2018 at 14:11
  • Hi There Yes I have ... still no correct answer and I am still struggling I would like to see if an answer specific to my situation would help Commented Apr 10, 2018 at 15:07
  • 1
    Show us what you searched, and what parts you don't understand. maybe you need just improve your Java (not JAVA) skills, follow some tutorials. Learn to walk before trying to run. Dynamic IDs is complicated problem, and Xpath is often wrong solution. Automate a page with static locators first. Commented Apr 10, 2018 at 15:19
  • 1
    I personally would be scared to death if I have to maintain tests containing such Xpath locator. Commented Apr 10, 2018 at 15:26

2 Answers 2

1

I would use the following xpath, assuming your webdriver is called "driver"

WebElement button = driver.findElement(By.xpath("//button[@aria-label='Show Calendar']"));

The idea is to try to locate an attribute that will not change, and ideally is unique. "aria-label" appears to be such an attribute, but you're only showing a segment of the page HTML, obviously, so you'd have to verify that it is unique.

0

Try this css selector:

button.ui-datepicker-trigger[aria-label='Show Calendar']

See if it matches only one element. Bellow is the Java code:

driver.findElement(By.cssSelector("button.ui-datepicker-trigger[aria-label='Show Calendar']"));

Some advise from me: never try to locate a web element by it's visible text because this is the most changeable thing in the web.

2
  • Hi Ches and Bill .. Thank you very much for the assistance , I have tried both of your code and it selects the element , however after it selects it closes ....I have this before with another element , however it was fixed by using JavascriptExecutor . which did not work in this case Commented Apr 11, 2018 at 9:26
  • post the exact error if it's still related to your question.
    – chesthar
    Commented Apr 11, 2018 at 15:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.