Home > Back-end >  Check AngularJS checkbox with Selenium
Check AngularJS checkbox with Selenium

Time:10-19

HTML Source here

It seems trivial, but after hours of attempts I cannot manage to check this checkbox using Selenium. I am able to select the element no problem, and have even been able to 'highlight' (see code snippet below) it by sending Keys.RETURN to the element, but on attempting to click it, nothing happens. Perhaps someone has an idea?

What I have tried already:

  • Using with WebDriverWait
  • Using multiple and different combinations of .click()/.send_keys(Keys.RETURN)
  • Just about every combination of XPATH/css selector/id/name/classname/text that Selenium would accept for the element, and all of its children and most of its parents (including spans/labels, and the text inside the label itself).
  • Directly clicking the element/label coordinates using actions (nothing happens, even when I can 100% confirm it is clicking the right spot by using context_click()).
  • Using execute_script to change the "checked" attribute to True (the checkbox appears checked, but it is clear it is only client side as a box that is supposed to render when it is actually clicked doesn't).
  • Using execute_script to change the class to 'ng-valid ng-not-empty ng-dirty ng-valid-parse ng-touched'

Here is the code that I feel got me the closest (it is 'highlighting' the box as seen here)

browser.find_element(By.ID, "sp_formfield_none_of_the_above").send_keys(Keys.RETURN)

CodePudding user response:

Managed to solve it after another while of testing. Hopefully this helps someone in the future. I had to use JS to execute .click() on the checkbox element. In retrospect, I should have tried this solution sooner. See code snippet:

cb_none = browser.find_element(By.ID, 'sp_formfield_none_of_the_above')
browser.execute_script("arguments[0].click();", cb_none)
  • Related