python - 在 Selenium 中单击图像映射
问题描述
我必须测试一个具有巨大图像的应用程序,但只有一小部分可以通过图像映射单击。
我已经尝试了一切来计算正确的位置并点击,但是点击在位置之外并且测试失败。
有人可以告诉我如何记录当前鼠标位置或如何在测试期间显示鼠标吗?
我正在使用动作链将鼠标光标移动到图像的中心,但是从那里我的所有计算都在图像映射矩形之外进行了单击。
请看下面我的代码片段:
el=driver.find_elements_by_xpath("/html/body/form/table/tbody/tr/td/img")[0]
#el=driver.find_elements_by_xpath("//html/body/map/area[2]")[0]
width=el.size["width"]
height=el.size["height"]
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element(el)
print driver.get_window_position()
action.move_by_offset(193, 310)
print driver.get_window_position()
action.click()
action.perform()
谢谢,
解决方案
只是作为参考。我最终找到了解决方案。
# Wait for the map to show up and navigate to account page
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "Map")))
except Exception, e:
print "Image with token toot long to load..."
raise AssertionError()
href = driver.find_element_by_xpath("//html/body/map/area[2]").get_attribute("href")
driver.get(href)
我可以看到单击只会触发对 HREF 的获取,因此我没有费力地找到确切的单击位置,而是将 href 放在 var 上,然后对该 href 进行获取。
脚本运行良好,甚至更好,它不依赖于屏幕分辨率。
推荐阅读
- mysql - 如何在 ASP.Net Core 中将 Linq Select 中的 2 个结果(添加列)组合成 1 个表或 CrossJoin
- python - 更快地遍历一个 DataFrame 的行以将列添加到第二个 DataFrame
- python - python检查任何已解析的字符串是否与数组匹配
- linux - 如何在 shell 脚本中访问“可变位置”命令行参数?
- reactjs - 是否可以从同一个自定义 Word 选项卡中调用 VSTO 插件和 Office.js 插件?
- php - 用于编辑数据库的表单操作
- linux - 为什么 os.OpenFile 不会创建 777 文件
- android - 如何在本机反应中创建自定义通知布局?
- javascript - 如何将用户外键设置为当前用户 django ajax
- python - kivymd MDbottomnavigation on_tab_press