如何在python中制定Xpath表达式定位饼图(洋葱圈)中的所有元素? [英] How to formulate an Xpath expression in python locate all the elements in a pie chart (onion ring)?

查看:29
本文介绍了如何在python中制定Xpath表达式定位饼图(洋葱圈)中的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<svg width="350" height="300"><g transform="translate(175,150)"><path d="M0,-140A140,140 0 0,1 0,-140L0,-90A90,90 0 0,0 0,-90Z" style="fill: rgb(158, 202, 225);">;</路径><path d="M0,-140A140,140 0 0,1 0,-140L0,-90A90,90 0 0,0 0,-90Z" style="fill: rgb(107, 174, 214);">;</路径><path d="M0,-140A140,140 0 0,1 134.55816974212192,-38.65357623364557L86.50168054850695,-24.84879,140 0,1 134.55816974212192,-38.65357623364557L86.50168054850695,-24.8487279060,-Z,0,0,400,0,400);"></path><路径d = M134.55816974212192,-38.65357623364557A140,140 0 0,1 134.55816974212192,-38.65357623364557L86.50168054850695,-24.848727578772156A90,90 0 0,0 86.50168054850695,-24.848727578772156Z" 风格=填充:RGB(33, 113, 181);"></path><路径d = M134.55816974212192,-38.65357623364557A140,140 0 0,1 -6.02311262063425,139.87037611431205L-3.872000970407732,89.91667035920061A90,90 0 0,0 86.50168054850695,-24.848727578772156Z" 风格=填充:RGB(8,81, 156);"></path><路径d = M-6.02311262063425,139.87037611431205A140,140 0 0,1 -29.89292816157139,136.77138898880526L-19.21688238958161,87.92446434994623A90,90 0 0,0 -3.872000970407732,89.91667035920061Z" 风格=填充:RGB(116,196, 118);"></path><路径d = M-29.89292816157139,136.77138898880526A140,140 0 0,1 -130.73721203520947,-50.07775343663755L-84.04535059406322,-32.19284149498128A90,90 0 0,0 -19.21688238958161,87.92446434994623Z" 风格=填充:RGB(65, 171, 93);"></path><路径d = M-130.73721203520947,-50.07775343663755A140,140 0 0,1 -130.73721203520947,-50.07775343663755L-84.04535059406322,-32.19284149498128A90,90 0 0,0 -84.04535059406322,-32.19284149498128Z" 风格=填充:RGB(35, 139, 69);"></path><路径d = M-130.73721203520947,-50.07775343663755A140,140 0 0,1 -35.7494613162662,-135.35869390473883L-22.981796560456843,-87.01630322447495A90,90 0 0,0 -84.04535059406322,-32.19284149498128Z" 风格=填充:RGB(0, 109, 44);"></path><路径d = M-35.7494613162662,-135.35869390473883A140,140 0 0,1 -2.5717582746235667e-14,-140L-1.6532731765437215e-14,-90A90,90 0 0,0 -22.981796560456843,-87.01630322447495Z" 风格="填充:RGB(158, 202, 225);"></path></svg>

请在附件中找到饼图和 HTML.

我试图将鼠标悬停在附加饼图中的所有段上以打印应用程序显示的工具提示消息.

我编写了以下代码,但出于某种原因,它仅将鼠标悬停在 1 个线段上.

for element in driver.find_elements_by_xpath("//div[@class='join-strength-chart']"):元素 = driver.find_element_by_tag_name('path')webdriver.ActionChains(driver).move_to_element(elements).perform()时间.sleep(2)tooltipmessage = driver.find_element_by_xpath("//div[@class='d3-tipjoined-tables-chart n']")打印 tooltipmessage.text

我确实尝试过使用 driver.find_elements_by_tag_name('path') 并且这也不起作用.

有人可以帮忙吗?谢谢

<块引用>

解决方案

XPath 表达式

//div[@class='join-strength-chart']

将返回具有 class 属性且值为 join-strength-chart

元素的节点集.如果你有很多节点集,你可以循环节点集(是这样吗?)

这个表达:

//div[@class='join-strength-chart']//路径

将返回那些

内的所有 元素的节点集.如果您在文件中只有该类的一个

,那么使用这个表达式是有意义的,因为它已经为您提供了所有的 元素.如果你有很多 join-strength-charts 可能没有,因为这将返回所有路径.

<div class="join-strength-chart"> <svg width="350" height="300"> <g transform="translate(175,150)"> <path d="M0,-140A140,140 0 0,1 0,-140L0,-90A90,90 0 0,0 0,-90Z" style="fill: rgb(158, 202, 225);"></path> <path d="M0,-140A140,140 0 0,1 0,-140L0,-90A90,90 0 0,0 0,-90Z" style="fill: rgb(107, 174, 214);"></path> <path d="M0,-140A140,140 0 0,1 134.55816974212192,-38.65357623364557L86.50168054850695,-24.848727578772156A90,90 0 0,0 0,-90Z" style="fill: rgb(66, 146, 198);"></path> <path d="M134.55816974212192,-38.65357623364557A140,140 0 0,1 134.55816974212192,-38.65357623364557L86.50168054850695,-24.848727578772156A90,90 0 0,0 86.50168054850695,-24.848727578772156Z" style="fill: rgb(33, 113, 181);"></path> <path d="M134.55816974212192,-38.65357623364557A140,140 0 0,1 -6.02311262063425,139.87037611431205L-3.872000970407732,89.91667035920061A90,90 0 0,0 86.50168054850695,-24.848727578772156Z" style="fill: rgb(8, 81, 156);"></path> <path d="M-6.02311262063425,139.87037611431205A140,140 0 0,1 -29.89292816157139,136.77138898880526L-19.21688238958161,87.92446434994623A90,90 0 0,0 -3.872000970407732,89.91667035920061Z" style="fill: rgb(116, 196, 118);"></path> <path d="M-29.89292816157139,136.77138898880526A140,140 0 0,1 -130.73721203520947,-50.07775343663755L-84.04535059406322,-32.19284149498128A90,90 0 0,0 -19.21688238958161,87.92446434994623Z" style="fill: rgb(65, 171, 93);"></path> <path d="M-130.73721203520947,-50.07775343663755A140,140 0 0,1 -130.73721203520947,-50.07775343663755L-84.04535059406322,-32.19284149498128A90,90 0 0,0 -84.04535059406322,-32.19284149498128Z" style="fill: rgb(35, 139, 69);"></path> <path d="M-130.73721203520947,-50.07775343663755A140,140 0 0,1 -35.7494613162662,-135.35869390473883L-22.981796560456843,-87.01630322447495A90,90 0 0,0 -84.04535059406322,-32.19284149498128Z" style="fill: rgb(0, 109, 44);"></path> <path d="M-35.7494613162662,-135.35869390473883A140,140 0 0,1 -2.5717582746235667e-14,-140L-1.6532731765437215e-14,-90A90,90 0 0,0 -22.981796560456843,-87.01630322447495Z" style="fill: rgb(158, 202, 225);"></path> </g> </svg> </div>

Please find attached is the pie chart and the HTML.

I am trying to hover over all the segments in the attached pie chart to print the tooltip message that the app shows.

I wrote the following code and it only hovers the mouse on 1 segment for some reason.

for elements in driver.find_elements_by_xpath("//div[@class='join-strength-chart']"):
            elements = driver.find_element_by_tag_name('path')
            webdriver.ActionChains(driver).move_to_element(elements).perform()
            time.sleep(2)
            tooltipmessage = driver.find_element_by_xpath("//div[@class='d3-tip joined-tables-chart n']")
            print tooltipmessage.text

I did try using driver.find_elements_by_tag_name('path') and that doesn't work either.

Can someone please help? Thank you

解决方案

The XPath expression

//div[@class='join-strength-chart']

will return a node-set of <div> elements that have a class attribute with the value join-strength-chart. You can loop the node-set if you have many of those (is that the case?)

This expression:

//div[@class='join-strength-chart']//path

will return a node-set of all the <path> elements that are inside those <div>s. If you only have one <div> of that class in the file, it would make sense to use this expression, since it already gives you all the <path> elements. If you have many join-strength-charts maybe not, since this will return all paths from all of them.

这篇关于如何在python中制定Xpath表达式定位饼图(洋葱圈)中的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆