Reputation: 21
Using python + Selenium Web Driver, I am attempting to automate selection of certain radio buttons for an insurance web site. Problem is that IDs of the radio buttons are dynamic. For example, when a new insurance application is generated, a new page is generated and the radio buttons on the screen have dynamically created IDs (see HTML example below)
<div class="data">
<input type="radio" id="subj_received_33965" name="subj_status[33965]" value="received" class="radio form-element-left-radio"><label for="subj_received_33965" class="label-right">Received</label>
<input type="radio" id="subj_waived_33965" name="subj_status[33965]" value="waived" class="radio form-element-left-radio multi-radio-line-spacer" checked=""><label for="subj_waived_33965" class="label-right">Waived</label>
<input type="radio" id="subj_open_33965" name="subj_status[33965]" value="open" class="radio form-element-left-radio multi-radio-line-spacer"><label for="subj_open_33965" class="label-right">Open</label>
</div>
So, using the above example, I want to click on radio button with id "subj_received_33965". However, because the elements are dynamically generated, the id will be different at the next automation run (and the script will fail).
What should I do to allow the script to consistently click ONLY the elements that begin with "subj_received_" on this page OR select only the elements that have the value = "received"?
Thanks
Upvotes: 1
Views: 1256
Reputation: 21
Input from alecxe helped me in finding a workable solution.
I also had the developer add an integer value to the id which helped in locating the correct id.
The following snippet works
button = self.driver.find_element_by_css_selector('[id*="2_subj_received_"]')
button.click()
Thanks.
Upvotes: 0
Reputation: 474131
You can always apply a partial match via a CSS selector or XPath:
driver.find_element_by_css_selector("input[id^=subj_received]")
driver.find_element_by_css_selector("input[id*=subj_received]")
where ^=
means "starts with", *=
means "contains".
If you want to check the value
attribute:
driver.find_element_by_css_selector("input[value=received]")
And you can check both:
driver.find_element_by_css_selector("input[value=received][id^=subj_received]")
Upvotes: 3