繁体   English   中英

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

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

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

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

<rect>

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

  • x :此属性确定矩形的x坐标。
    • 值类型:| ; 默认值:0 动画:是
  • y :此属性确定矩形的y坐标。
    • 值类型:| ; 默认值:0 动画:是
  • width :此属性确定矩形的宽度。
    • 值类型:自动|| ; 默认值:自动; 动画:是
  • height :此属性确定矩形的高度。
    • 值类型:自动|| ; 默认值:自动; 动画:是
  • rx :此属性确定矩形的水平角半径。
    • 值类型:自动|| ; 默认值:自动; 动画:是
  • ry :此属性确定矩形的垂直角半径。
    • 值类型:自动|| ; 默认值:自动; 动画:是
  • pathLength :此属性允许以用户单位指定路径的总长度。
    • 值类型: ; 默认值:无; 动画:是

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


这个用例

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

  • 对于<svg>元素:

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

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

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

参考文献

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

使用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()

要么

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