如何通过Selenium-Python访问'rect'类型元素 [英] How to access to 'rect' type element through Selenium-Python

查看:191
本文介绍了如何通过Selenium-Python访问'rect'类型元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dom中有一个rect对象:

There is a rect object in the dom:

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

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

I am trying to search it with following code:

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

这不起作用.

但是以下内容确实存在:

But the following does:

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

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

Any clues on why the first one does not work?

推荐答案

<rect>

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

<rect>

The <rect> element is a basic SVG shape that creates rectangles, defined by their corner's position, their width, and their height. The rectangles may have their corners rounded.

一个例子:

<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>


属性

属性 >元素如下:


Attributes

The attributes of <rect> elements are as follows:

  • x:此属性确定矩形的x坐标.
    • 值类型:| ;预设值:0;动画:是
    • x: This attribute determines the x coordinate of the rect.
      • Value type: | ; Default value: 0; Animatable: yes
      • 值类型:| ;预设值:0;动画:是
      • 值类型:自动|| ;默认值:自动;动画:是
      • 值类型:自动|| ;默认值:自动;动画:是
      • 值类型:自动|| ;默认值:自动;动画:是
      • 值类型:自动|| ;默认值:自动;动画:是
      • 值类型:;默认值:无;动画:是

      注意:从SVG2开始,x,y,宽度,高度,rx和ry是几何属性,这意味着这些属性也可以用作该元素的CSS属性.

      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>元素是 SVG 元素,因此要定位此类元素,您必须明确指定 SVG名称空间使用如下:


      This usecase

      As the <rect> element is a SVG element so to locate such elements you have to explicitly specify the SVG namespace when accessing the elements using xpath as follows:

      • 对于<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

      这篇关于如何通过Selenium-Python访问'rect'类型元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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