andhaa
andhaa

Reputation: 33

Python & Selenium - Find element by label text

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?

Upvotes: 1

Views: 4463

Answers (2)

rahul rai
rahul rai

Reputation: 2326

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.

Upvotes: 0

Try the below xpath

//label[contains(text(), 'Delete group')]//ancestor::div//input

Upvotes: 1

Related Questions