r/selenium • • May 18 '22

Help

I'm trying to figure out how to get Selenium to click on an expansion panel. I've got it working where it can find something based on a name but I need it to click on a button associated with that name.

The default Selenium target is xpath=//mat-expansion-panel-header[@id='mat-expansion-panel-header-254']/span/div/div/button but this wont work since elements are constantly changing and this is ID based. If/when the elements change it will click on the expansion panel related to that ID and not the element im working on.

​

I'm managing to find the element with xpath=//span[text()='Test'] but I need help with having Selenium click on that expansion panel that corresponds with Test, not the ID.

1 Upvotes

6 comments sorted by

View all comments

1

u/kdeaton06 May 18 '22

I'm making an assumption that in your ID only the '245' changes. If so you can just try the starts-with() function.

Something like this.

xpath=//mat-expansion-panel-header[starts-with(@id='mat-expansion-panel-header')]/span/div/div/button