python - 如何检查特定元素的复选框?
问题描述
无法选中属于特定图像的复选框。 https://i.stack.imgur.com/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()
解决方案
您不会根据iname
. 你总是点击checkbox
。您应该能够使用下面的 XPath 定位器找到“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
使用该定位器(修改为查找image_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()
注意:您可能需要等待元素可单击...然后单击它。
推荐阅读
- node.js - 如何在给定方位角和高度的情况下获得太阳的 3D 坐标(XYZ)?
- java - 父类定义的方法如何在子类对象中被调用?
- android - 是否有必要通过弹出窗口显示有关位置(而非背景)权限的显着披露?
- c - C中关于动态内存分配的技术问题
- python - 使用 Python 创建类的属性错误
- android-safe-args - 使用 Safe Args 时添加 android:defaultValue="@null" 后无法构建应用程序
- python - 为什么 isinstance() 函数不起作用?
- python - 如何使用 xlsxwriter 将多个元素写入一个单元格?
- textfield - max_height 不适用于 KivyMD 中的 MDTextField
- java - java - 如何使用java获取具有特定内容的文件名?