Python Selenium 模拟点击一个鼠标悬停才显示的SVG时失败
如题在使用Selenium 自动化时 有一个开关按钮是svg ,鼠标要悬停才会显示该SVG,模拟点击时发现点击没有反应
代码实现
我的思路是找到这个需要悬停的元素后,再点击这个SVG,因为只要这个元素悬停后,这个开关SVG就会显示 代码如下
:
trs = main.browser.find_elements(By.CSS_SELECTOR,'#root > div.fc-manage-dashboard-container > div.manage-dashboard-main-container > div:nth-child(2) > div > div.manage-dashboard-main-workspace-content > div.brave-the-winds-common-libs-table-list-custom-container > div.one-table-wrapper.brave-the-winds-common-libs-table-list.one-table-wrapper-normal > div > div > div > div.one-table-scroll > div.one-table-body > table > tbody > tr')
# test= trs[1].find_element(By.CSS_SELECTOR,'td:nth-child(4) > div > div > svg')
test = main.browser.find_element(By.CSS_SELECTOR,'#root > div.fc-manage-dashboard-container > div.manage-dashboard-main-container > div:nth-child(2) > div > div.manage-dashboard-main-workspace-content > div.brave-the-winds-common-libs-table-list-custom-container > div.one-table-wrapper.brave-the-winds-common-libs-table-list.one-table-wrapper-normal > div > div > div > div.one-table-scroll > div.one-table-body > table > tbody > tr:nth-child(2) > td:nth-child(4) > div > div > svg')
print("悬停在计划按钮上")
# ActionChains(main.browser).move_to_element(trs[1]).perform()
ActionChains(main.browser).move_to_element(test).move_by_offset(5,5).click().perform()
# sleep(0.5)
print("点击开启计划")
日志图片
然后发现开关的状态并没有改变,点击无效,不知道什么原因,先记录下来,找到原因再更新
9月6日更新
问题解决,但是知其然,不知其所以然,重复点击三次才会生效,这个按钮好像做了奇奇怪怪的处理,点击区域也必须是元素中间,总之代码改成下面之后问题解决:
for i in range(0,len(trs)):
if i == 0:
continue
else:
print("定位元素")
buttonsvg = trs[i].find_element(By.CSS_SELECTOR,'td:nth-child(4) > div > div > svg')
print("悬停在计划按钮上并点击3次后生效")
main.browser.execute_script(
"arguments[0].setAttribute('style', arguments[1]);",
buttonsvg,
"border: 2px solid red;" #边框border:2px; red红色
)
action.move_to_element(buttonsvg).move_by_offset(9,7).click().perform()
action.move_to_element(buttonsvg).move_by_offset(9,7).click().perform()
action.move_to_element(buttonsvg).move_by_offset(9,7).click().perform()
sleep(1)