Auto4Ever
Auto4Ever

Reputation: 9

How can I check a checkbox for a specific element?

Unable to check the checkbox that belong to a specific image. https://i.sstatic.net/upIYA.png

def select_image_in_drawer(self, image_name):

    checkbox = self.driver.find_element(*EditSpecificationLocators.DRAWER_IMAGE_CHECKBOX)
    drawer_image = self.driver.find_elements(*EditSpecificationLocators.DRAWER_IMAGE_NAME)
    for iname in drawer_image:
        tag_val = iname.text # get text of an element
        if image_name == iname.text:
            print(tag_val)
            checkbox.click()

Upvotes: 0

Views: 75

Answers (1)

JeffC
JeffC

Reputation: 25587

You aren't changing your locator based on iname. You always click checkbox. You should be able to use the XPath locator below to find the checkbox for "kyoScan-4"

//div[@class='d-flex h-100 p-2'][.//div[.='kyoScan-4']]//span[@class='p-checkbox-icon p-c']
^ find a DIV
     ^ that contains this class
                                ^ that has a descendant DIV
                                       ^ that contains text kyoScan-4
                                                       ^ then find the descendant SPAN
                                                             ^ that has this class

With that locator (modified to look for image_name as the contained text), you can simplify your method to just a one-liner

def select_image_in_drawer(self, image_name):
    self.driver.find_element_by_xpath("//div[@class='d-flex h-100 p-2'][.//div[.='" + image_name + "']]//span[@class='p-checkbox-icon p-c']").click()

NOTE: You may need to wait for the element to be clickable... then click it.

Upvotes: 1

Related Questions