如何通过 Selenium-Python 访问“rect"类型元素 [英] How to access to 'rect' type element through Selenium-Python
问题描述
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()
这不起作用.
但以下是:
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?
推荐答案
元素是一个基本的 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>
<小时>
属性
属性>
元素如下:
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.
<小时>
这个用例
由于
元素是一个 SVG 元素,因此要定位此类元素,您必须明确指定 SVG 命名空间 使用 xpath 如下:
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:对于
元素:
//*[name()="svg"]
对于
元素://*[name()="svg"]/*[name()="g"]
对于
元素://*[name()="svg"]/*[name()="g"]/*[name()="rect"] //*[name()="svg"]/*[name()="rect"]
您可以在
这篇关于如何通过 Selenium-Python 访问“rect"类型元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!