首页 > 解决方案 > Selenium Python:等待元素的背景颜色为'rgb(...)`

问题描述

使用 Selenium 和 Python,我们如何等待具有 id 的某个元素foo具有backgroundColorof "rgb(1, 2, 3)"

到目前为止,我认为它应该是这样的

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

expected_backgroundColor = EC.some_method((By.ID, "foo"))
WebDriverWait(self.driver, 15).until(expected_backgroundColor)

但不确定EC使用哪种方法。

任何建议都会有所帮助!谢谢!

标签: pythonpython-3.xseleniumselenium-webdriverselenium-chromedriver

解决方案


您可以为此使用presence_of_element_locatedorvisibility_of_element_located方法。让我们看看下面的例子,

预期元素xPath

//input[@id='rgb(1, 2, 3)']

代码

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//input[@id='rgb(1, 2, 3)']')))

或者

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//input[@id='rgb(1, 2, 3)']')))

在 Java 中, 和 方法等attributeToBe方法attributeContains可用attributeNotToBeEmpty,但不确定这些方法在 Python 中是否可用。

Java 示例

WebElement element = driver.findElement(By.xpath("xPath "));
wait.until(ExpectedConditions.attributeToBe(element, "id", "rgb(1, 2, 3)"));

推荐阅读