大哥哥们,帮小妹子解答下疑惑呗
解决了吗?我也遇到这个问题,我就是先定位上级,然后定位右侧内容的时候不行?
估计是你没有加等待时间吧,如果上一层级的点击之后响应的比较慢的话,你这边就发现不了下级的li的位置了。
先通过元素定位找到“客户管理”,点开菜单之后,通过linktext或者xpath查找
先点击菜单图标 ,你这明显 是找不到元素啊
先找到菜图标的位置点击,再找到ul
类似这样 :ul = driver.find_element_by_xpath('*************')
然后lis = ul.find_elements_by_xpath('li')看一下 有几个li
print(len(lis))
如果 打印个数正确,就可以定位你想要 的li的位置
估计是你没有加等待时间吧,如果上一层级的点击之后响应的比较慢的话,你这边就发现不了下级的li的位置了。
我也是遇到了这种问题,也是定位ul标签下的li标签
一看就是刚学的,竟然不是手写的xpath,这样当前端改点啥的时候,你都得大改元素定位
xpath=//固定节点//ul[@class=“XXX”]/li[6]/a
/下一节点
//下面不一定多少层级的节点
following-sibling::*兄弟节点
./上一层级节点
试着这么抓一下
给个例子
以下例子是选择枚举值后,点击查询
验证点位是否有要选的枚举值,
定位xpath从html开始的根本没法维护的,因为研发的需求一直在变,变一点xpath就会变
print(synchronizationTime2)
self.driver.find_element_by_xpath('//input[@placeholder="开始时间"]').send_keys(synchronizationTime2)
self.time.check_time1()
self.driver.find_element_by_xpath('//input[@placeholder="结束时间"]').send_keys(synchronizationTime2)
self.time.check_time1()
self.driver.find_element_by_xpath(
'//label[text()="同步分类"]/following-sibling::*//input[@placeholder="请选择"]').click()
self.time.check_time1()
enum1 = self.driver.find_element_by_xpath('//div[@x-placement="bottom-start"]//ul')
enumcount1 = enum1.find_elements_by_tag_name('li')
number = len(enumcount1)
print(number)
# 遍历同步分类枚举值,如果有想选择的同步分类值,选择
for i in range(1, number + 1):
enumname1 = self.driver.find_element_by_xpath(
'//div[@x-placement="bottom-start"]//li[' + str(i) + ']/span').text
print("同步分类枚举值",enumname1)
if enumname1 == synchronizationClassify:
self.driver.find_element_by_xpath('//div[@x-placement="bottom-start"]//li[' + str(i) + ']/span').click()
self.time.check_time1()
print(i)
break
else:
result = "没有枚举值"
self.assertEqual(result, "得有枚举值", "同步分类枚举值无值")
#数据源枚举值选择
self.driver.find_element_by_xpath(
'//label[text()="数据源"]/following-sibling::*//input[@placeholder="请选择"]').click()
self.time.check_time1()
enum2 = self.driver.find_element_by_xpath('//div[@x-placement="bottom-start"]//ul')
enumcount2 = enum2.find_elements_by_tag_name('li')
number2 = len(enumcount2)
print(number2)
# 遍历同步分类枚举值,如果有想选择的同步分类值,选择
for i in range(1, number2 + 1):
enumname2 = self.driver.find_element_by_xpath(
'//div[@x-placement="bottom-start"]//li[' + str(i) + ']/span').text
print("数据源枚举值",enumname2)
if enumname2 == dataSource:
self.driver.find_element_by_xpath('//div[@x-placement="bottom-start"]//li[' + str(i) + ']/span').click()
self.time.check_time1()
print(i)
break
else:
result = "没有枚举值"
self.assertEqual(result, "得有枚举值", "数据源枚举值无值")
self.driver.find_element_by_xpath('//span[text()="查询"]').click()