Python 如何选中特定元素的复选框?

Python 如何选中特定元素的复选框?,python,selenium,Python,Selenium,无法选中属于特定图像的复选框。 您没有根据iname更改定位器。您总是单击复选框。您应该能够使用下面的XPath定位器找到“kyoScan-4”的复选框 使用该定位器(修改为查找image\u name作为包含的文本),您可以将方法简化为一行 def select_image_in_drawer(self, image_name): self.driver.find_element_by_xpath("//div[@class='d-flex h-100 p-2'][.//div[.=

无法选中属于特定图像的复选框。


您没有根据iname更改定位器。您总是单击
复选框
。您应该能够使用下面的XPath定位器找到“kyoScan-4”的复选框

使用该定位器(修改为查找
image\u name
作为包含的文本),您可以将方法简化为一行

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()
注意:您可能需要等待元素可单击。。。然后单击它。

请阅读原因。粘贴代码并正确格式化。
//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
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()