微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过Selenium-Python访问’rect’类型元素

dom中有一个直肠对象:

<rect class="slv-blank" id="id123" height="8.8" stroke-width="1px" width="18.8" x="59.2" y="37.5"></rect>

我正在尝试使用以下代码进行搜索

webdriverwait(driver, 1).until(ec.presence_of_element_located(("xpath", '//rect[@id="id123"]'))).click()

这是行不通的.

但是以下内容可以:

webdriverwait(driver, 1).until(ec.presence_of_element_located(("xpath", '//*[name()="rect"][@id="id123"]'))).click()

关于第一个为什么不起作用的任何线索?

解决方法:

&LT RECT&GT

<rect>元素是基本的SVG形状,可创建矩形,矩形由其角的位置,宽度和高度定义.矩形的角可能会变圆.

一个例子:

<svg viewBox="0 0 220 100" xmlns="http://www.w3.org/2000/svg">
  <!-- Simple rect element -->
  <rect x="0" y="0" width="100" height="100" />

  <!-- Rounded corner rect element -->
  <rect x="120" y="0" width="100" height="100" rx="15" ry="15" />
</svg>

属性

< rect>的attributes表示元素如下:

> x:此属性确定矩形的x坐标.

>值类型:| ;认值:0动画:是

> y:此属性确定矩形的y坐标.

>值类型:| ;认值:0动画:是

> width:此属性确定矩形的宽度.

>值类型:自动|| ;认值:自动;动画:是

> height:此属性确定矩形的高度.

>值类型:自动|| ;认值:自动;动画:是

> rx:此属性确定矩形的水平角半径.

>值类型:自动|| ;认值:自动;动画:是

> ry:此属性确定矩形的垂直角半径.

>值类型:自动|| ;认值:自动;动画:是

> pathLength:此属性允许以用户单位指定路径的总长度.

>值类型:;认值:无;动画:是

Note: Starting with SVG2 x, y, width, height, rx and ry are Geometry Properties, meaning those attributes can also be used as CSS properties for that element.

这个用例

作为< rect> element是SVG元素,因此要定位此类元素,您必须在使用访问元素时明确指定SVG namespace,如下所示:

>对于< svg>内容

//*[name()="svg"]

>对于< g>内容

//*[name()="svg"]/*[name()="g"]

>对于< rect>内容

//*[name()="svg"]/*[name()="g"]/*[name()="rect"]
//*[name()="svg"]/*[name()="rect"]

参考

您可以在中找到几个相关的详细讨论

> How to click on SVG elements using XPath and Selenium WebDriver through Java
> Unable to locate SVG elements through xpath on Kendo UI chart

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐