📜  如何使用量角器等待元素的属性更改为特定值?(1)

📅  最后修改于: 2023-12-03 15:38:08.760000             🧑  作者: Mango

如何使用量角器等待元素的属性更改为特定值?

在编写自动化测试脚本时,常常需要等待某个元素的属性值更改为特定值之后再进行后续操作。这时候,我们可以使用Selenium提供的Expected Conditions结合WebDriverWait来等待元素属性的变化。

具体实现可以参考以下步骤:

  1. 首先,需要导入ExpectedConditionsWebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  1. 然后,可以使用WebDriverWait等待元素的某个属性值更改为特定值。例如,我们等待元素的class属性值变为active
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'element_id')))
wait.until(EC.attribute_to_be(By.ID, 'element_id', 'class', 'active'))

在这里,我们首先等待元素可点击,然后继续等待元素class属性变为active

  1. 如果需要等待其它属性值的变化,可以使用类似的方式。例如,我们等待元素value属性变为123
wait.until(EC.attribute_to_be(By.ID, 'element_id', 'value', '123'))
  1. 最后,如果需要等待多个属性值的变化,可以使用EC.and_函数:
wait.until(EC.and_(EC.attribute_to_be(By.ID, 'element_id', 'class', 'active'), EC.attribute_to_be(By.ID, 'element_id', 'value', '123')))

在这里,我们等待元素class属性为activevalue属性为123

这样,就可以利用Expected ConditionsWebDriverWait等待元素属性的变化了。

以上为如何使用量角器等待元素的属性更改为特定值的介绍。