Py学习  »  Python

Senium Python:理解对“rect”类型元素的访问

Dick_Grayson • 6 年前 • 2103 次点击  

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

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

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54071
文章 [ 2 ]  |  最新文章 6 年前
KunduK
Reply   •   1 楼
KunduK    7 年前

使用 Action 类或 JavaScript 执行人。

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)
DebanjanB
Reply   •   2 楼
DebanjanB    7 年前

<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 属于 <矩形>

  • x :此属性确定矩形的x坐标。
  • y :此属性确定矩形的y坐标。
    • 值类型:|;默认值:0;可设置动画:是
  • width
    • 值类型:auto | |;默认值:auto;可设置动画:yes
  • height :此属性确定矩形的高度。
    • 值类型:auto | |;默认值:auto;可设置动画:yes
  • rx
    • 值类型:auto | |;默认值:auto;可设置动画:yes
  • ry
    • 值类型:auto | |;默认值:auto;可设置动画:yes
  • pathLength :此属性允许以用户单位指定路径的总长度。
    • 值类型:;默认值:无;可设置动画:是

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


作为 <矩形> 元素是 元素,因此要定位此类元素,必须显式指定 SVG namespace 具体如下:

  • 为了 <svg>

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

    //*[name()="svg"]/*[name()="g"]
    
  • 为了 <矩形>

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

工具书类

您可以在中找到一些相关的详细讨论