[英]How to check if an element is having an attribute using selenium-python
[英]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坐標。
y
:此屬性確定矩形的y坐標。
width
:此屬性確定矩形的寬度。
height
:此屬性確定矩形的高度。
rx
:此屬性確定矩形的水平角半徑。
ry
:此屬性確定矩形的垂直角半徑。
pathLength
:此屬性允許以用戶單位指定路徑的總長度。
注意 :從SVG2開始,x,y,寬度,高度,rx和ry是幾何屬性,這意味着這些屬性也可以用作該元素的CSS屬性。
由於<rect>
元素是SVG元素,因此要定位此類元素,您必須在使用xpath訪問元素時明確指定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.