简体   繁体   English

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

[英]How to access to 'rect' type element through Selenium-Python

There is a rect object in the dom: 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()

This does not work. 这是行不通的。

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>

The <rect> element is a basic SVG shape that creates rectangles, defined by their corner's position, their width, and their height. <rect>元素是基本的SVG形状,可创建矩形,该矩形由其角的位置,宽度和高度定义。 The rectangles may have their corners rounded. 矩形的角可能会变圆。

An Example: 一个例子:

<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: <rect>元素的属性如下:

  • x : This attribute determines the x coordinate of the rect. x :此属性确定矩形的x坐标。
    • Value type: | 值类型:| ; ; Default value: 0; 默认值:0 Animatable: yes 动画:是
  • y : This attribute determines the y coordinate of the rect. y :此属性确定矩形的y坐标。
    • Value type: | 值类型:| ; ; Default value: 0; 默认值:0 Animatable: yes 动画:是
  • width : This attribute determines the width of the rect. width :此属性确定矩形的宽度。
    • Value type: auto|| 值类型:自动|| ; ; Default value: auto; 默认值:自动; Animatable: yes 动画:是
  • height : This attribute determines the height of the rect. height :此属性确定矩形的高度。
    • Value type: auto|| 值类型:自动|| ; ; Default value: auto; 默认值:自动; Animatable: yes 动画:是
  • rx : This attribute determines the horizontal corner radius of the rect. rx :此属性确定矩形的水平角半径。
    • Value type: auto|| 值类型:自动|| ; ; Default value: auto; 默认值:自动; Animatable: yes 动画:是
  • ry : This attribute determines the vertical corner radius of the rect. ry :此属性确定矩形的垂直角半径。
    • Value type: auto|| 值类型:自动|| ; ; Default value: auto; 默认值:自动; Animatable: yes 动画:是
  • pathLength : This attribute lets specify the total length for the path, in user units. pathLength :此属性允许以用户单位指定路径的总长度。
    • Value type: ; 值类型: ; Default value: none; 默认值:无; Animatable: yes 动画:是

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


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 as follows: 由于<rect>元素是SVG元素,因此要定位此类元素,您必须在使用访问元素时明确指定SVG命名空间 ,如下所示:

  • For <svg> elements: 对于<svg>元素:

     //*[name()="svg"] 
  • For <g> elements: 对于<g>元素:

     //*[name()="svg"]/*[name()="g"] 
  • For <rect> elements: 对于<rect>元素:

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

References 参考文献

You can find a couple of relevant detailed discussions in 您可以在中找到几个相关的详细讨论

Use Action class or JavaScript Executor. 使用Action类或JavaScript Executor。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.action_chains import ActionChains
elememnt=WebDriverWait(driver, 1).until(ec.presence_of_element_located(("xpath", '//*[@id="id123"]')))
ActionChains(driver).move_to_element(elememnt).click().perform()

OR 要么

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.action_chains import ActionChains
elememnt=WebDriverWait(driver, 1).until(ec.presence_of_element_located(("xpath", '//*[@id="id123"]')))
driver.execute_script("arguments[0].click();",elememnt)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM