Home > Software design >  Un-able to check status of a check box using selenium and python
Un-able to check status of a check box using selenium and python

Time:07-13

I am new to python, and I am trying to write a script to verify a check-box has been checked/enabled. My problem is that I am not able to read the correct value after the check box has been checked/selected or un-checked/un-selected.

I was able to locate the check box, select/enable it, then click "apply" to confirm the change. But when I tried to verify the check box's status, I could not get the correct value. If I used .is_enabled(), the read value would always be "True" no matter what the check box has been selected/enabled or not. And If I used .is_selected(), the value would always be "False".

Please help. Below is my code:

Locate_Checkbox = Browser.find_element(By.XPATH,'//*[@id="at-contain"]/sd-canvas-home/div/span/div[3]/sd-canvas/div/div/at-checkbox[11]/div/div')

Ranging = Locate_Checkbox.is_selected() 

print(Ranging)

I really appreciate your help.

CodePudding user response:

you can try this?

Locate_Checkbox = Browser.find_element(By.XPATH,'//*[@id="at-contain"]/sd-canvas-home/div/span/div[3]/sd-canvas/div/div/at-checkbox[11]')
Ranging = Locate_Checkbox.is_selected()

print(Ranging)

CodePudding user response:

is_enabled() is used to check whether element is connected and enable to perform the operation in the current context of the page. And NOT to check whether it is selected or not.

is_selected() is used to check whether the element is selected or not. This method is used on checkboxes, input boxes, radio buttons.

In your case you should rely on the is_selected() However if you use this function with find_element() it will never reach to the is_selected() method if in case of you have exception in finding the element. Probably that is what is happening in your case. To solve this problem you will have to use find_element**s**() variant. This will return you empty list incase of exception in finding element. for example.

locate_Checkbox = Browser.find_elements(By.XPATH,'//*[@id="at-contain"]/sd-canvas-home/div/span/div[3]/sd-canvas/div/div/at-checkbox[11]/div/div')

if len(locate_Checkbox)>1:
    Ranging = locate_Checkbox[0].is_selected() 

print(Ranging)
  • Related