1

Im trying to locate and click an element (checkbox) from a big selection of checkboxes on a html site using python and selenium webdriver. HTML code looks like this:

HTML Code

        <div class="checkbox-inline col-md-5 col-lg-3 col-sm-6 m-l-sm rightCheckBox">
            <input type="checkbox" checked="checked" class="i-checks" name="PanelsContainer:tabsContentView:5:listTabs:rights-group-container:right-type-view:2:right-view:2:affected-right" disabled="disabled" id="id199"> <label>Delete group</label>
        </div>

My problem is that the only unique identifier is:

<label>Delete group</label>

All other elements/id's/names are used by other checkboxes or changes from page to page. I have tried the following code:

driver.find_element_by_xpath("//label[contains(text(), 'Delete group')]").click()

But I only get error when using this. Error: element not interactable

Anyone able to help with this?

andhaa
  • 33
  • 1
  • 8

2 Answers2

1

Try the below xpath

//label[contains(text(), 'Delete group')]//ancestor::div//input
  • Still getting error: Message: element not interactable code: driver.find_element_by_xpath("//label[contains(text(), 'Delete group')]//ancestor::div//input") – andhaa Aug 18 '20 at 11:03
  • Set [implicit](https://www.geeksforgeeks.org/implicit-waits-in-selenium-python/) wait before launching the URL – Mohamed Sulaimaan Sheriff Aug 18 '20 at 11:22
  • Still getting: element not interactable. Thanks for helping :) – andhaa Aug 18 '20 at 11:35
  • @andhaa if you can give me the website url – Mohamed Sulaimaan Sheriff Aug 18 '20 at 11:41
  • Im sorry I cant, since its on an internal network. I can give you more outputs/html code if that helps – andhaa Aug 18 '20 at 11:49
  • @andhaa try by giving [pageload timeout](https://stackoverflow.com/questions/17533024/how-to-set-selenium-python-webdriver-default-timeout). – Mohamed Sulaimaan Sheriff Aug 18 '20 at 11:55
  • Try with `//label[contains(text(), 'Delete group')]//ancestor::div[1]` and then `click`. The message clearly says that the element does exist but it's not intractable, so you can try webdriver wait until clickable or use Javascript clicking on `div` does not help. – supputuri Aug 18 '20 at 12:00
  • @supputuri when I try your way I get a different error: move_to requires a WebElement MohamedSulaimaanSheriff Seems waiting doesnt help with anything. Its allmost like its not clickable at all but it is a checkbox for sure. Pasted HTML code vs print screen, could there be anything missing or something? – andhaa Aug 18 '20 at 12:59
  • @andhaa Man,there is `disabled="disabled"` attribute try by removing this – Mohamed Sulaimaan Sheriff Aug 18 '20 at 13:01
  • @MohamedSulaimaanSheriff Its strange that its there when I copied html code from inspect, because its not there when I view elements while browsing. Its not in the screen shot either. So dont think its really there. – andhaa Aug 18 '20 at 13:08
  • @andhaa I can help you only if the website is accessible by me – Mohamed Sulaimaan Sheriff Aug 18 '20 at 13:09
  • `move_to` require a webElement - how are you trying to click? – supputuri Aug 18 '20 at 13:31
  • @supputuri I tried the following: //label[contains(text(), 'Delete group')]//ancestor::div[1]").click() And; webdriver.ActionChains(driver).move_to_element(element ).click(element ).perform() – andhaa Aug 19 '20 at 06:28
0

Try with Javascript.

checkBox = driver.find_element_by_xpath("//label[text()='Delete group']//ancestor::div//input")
# Scroll to checkbox if its not in screen
driver.execute_script("arguments[0].scrollIntoView();", checkBox)
driver.execute_script("arguments[0].click();", checkBox)

Note : As per HTML shared by you, checkbox is in Disabled state, so i am not sure click will trigger any action. However above code will click your checkbox.

rahul rai
  • 2,260
  • 1
  • 7
  • 17
  • Hi and thanks for your code. But didnt work. Found what I belive is the issue tho. Theres more then one "Delete group" label in the whole HTML code. The one Im trying to check is the second one, but my current code obviously tries to check the first one thats disabled. Need to find a way to tell the code to check the second "Delete group" label and not the first. – andhaa Aug 19 '20 at 14:18
  • Yes in that case you need to share HTML DOM for checkbox you want to click. Also just in case if all of property are similar to HTML you have shared, then you can simply index (if no other difference) is there. Something like **checkBox = driver.find_element_by_xpath("(//label[text()='Delete group'])[2]//ancestor::div//input")** – rahul rai Aug 19 '20 at 15:05