1

How do I select a radio button in java selenium webdriver that has an appended dynamic id. I don't have the ability to determine before hand the number that is appended. So I need a way to select via partially matching text while ignoring that last number.

id=edit-emergency-notification-status-existing-message-7

!"7" is generated with each new item created in this screen 1

2 Answers 2

2

Get it by xpath with the help of starts-with():

//*[starts-with(@id, "edit-emergency-notification-status-existing-message-")]
Sign up to request clarification or add additional context in comments.

4 Comments

I am getting: org.openqa.selenium.InvalidSelectorException: The given selector [starts-with(@id, "edit-emergency-notification-status-existing-message-")] is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Unable to locate an element with the xpath expression [starts-with(@id, "edit-emergency-notification-status-existing-message-")] because of the following error: SyntaxError: The expression is not a legal expression.
@3ck are you sure you are using the same exact expression as in the answer?
I am using it in a By: private By alertRadioButton = By.xpath("[starts-with(@id, \"edit-emergency-notification-status-existing-message-\")]");
@3ck and what about the //* part at the beginning?
1

Try CSS selector and use starts with wild character (^)

driver.findElement(By.cssSelector("input[id^='edit-emergency-notification-status-existing-message']"));

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.