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

标签 python selenium svg xpath svg-rect

dom中有一个rect对象:

<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>
<小时/>

属性

attributes<rect>要素如下:

  • 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>元素是一个 SVG 元素,因此要找到此类元素,您必须显式指定 SVG namespace使用 访问元素时如下:

  • 对于 <svg>元素:

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

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

    //*[name()="svg"]/*[name()="g"]/*[name()="rect"]
    //*[name()="svg"]/*[name()="rect"]
    
<小时/>

引用文献

您可以在

中找到一些相关的详细讨论

关于python - 如何通过Selenium-Python访问 'rect'类型元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58814174/

相关文章:

python - 有时 pip install 很慢

javascript - 使用 Capybara 和 Minitest 测试 JavaScript

html - 无法使用 XPath 按索引查找元素

Python 视频处理

python - Airflow :运行一次 Airflow 子标记的模式

javascript - 使用链接 rel ="import"将 SVG 加载到页面中

JavaFX 2.2 中的 SVG 图像

html - SVG 负载过大

python - 获取顶级 Python 脚本的路径

c# - C# 中的 Selenium 框架