How to extract input id of a checkbox in selenium using Python?

Viewed 57

I am trying to extract if one checkbox is checked or not using below code:

element = driver.find_element_by_xpath("//*[@id='checkbox-410']")
print (element.is_selected())

The problem i am facing is the id for that checkbox is updating every time as i need to do this multiple times. Below is html code where it was at once 339 and next it becomes some different number

<input id="checkbox-339" type="checkbox" label="Stoploss" class="su-checkbox" value="true">

Is it possible if i can check whether checkbox is checked or not using div, I mean there is only one checkbox in that div:

//*[@id="app"]/form/section/div[3]/div/div[2]/div[1]

Or if any other way to do this, please suggest?

2 Answers

Instead of

element = driver.find_element_by_xpath("//*[@label='checkbox-410']")

Try using

element = driver.find_element_by_xpath("//input[@id='Stoploss")

There are multiple ways to handle this :

//input[@label='Stoploss']

or if you wanna stick with id, maybe this could help :-

//input[contains(@id, 'checkbox-')]

and if there are multiple entries with //input[contains(@id, 'checkbox-')], you can apply xpath index as well.

(//input[contains(@id, 'checkbox-')])[1] 

or

(//input[contains(@id, 'checkbox-')])[2]

or so on..

Related