如何通过类名称和特定属性名称同时定位元素 [英] How to locate element by class name and specific attribute name at the same time
问题描述
我正在使用selenim python,并想找到以下元素:
I'm using selenim python,and want to locate the following element:
<div id="coption5" class="copt" style="display: block;">
我需要类名'copt'和样式值"display:block;" ,有什么办法可以通过类名和属性值同时显示?
I need both the class name 'copt' and style value "display: block;",is there any way I can locate this element with both class name and attribute value at the same time?
谢谢!
推荐答案
如果考虑 style 值 display: block;
是强制性的,则可以引入 WebDriverWait 作为visibility_of_element_located()
,您可以使用以下任一定位器策略:
Incase considering the style value display: block;
is mandatory you can induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following Locator Strategies:
-
使用
CSS_SELECTOR
:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.copt[id^='coption']")))
使用XPATH
:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='copt' and starts-with(@id, 'coption')]")))
注意:您必须添加以下导入:
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
这篇关于如何通过类名称和特定属性名称同时定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!