Selenium Python:等待元素的背景颜色为'rgb(...)`



使用Selenium和Python,我们如何等待id为foo的某个元素的backgroundColor"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)

但不确定使用哪种CCD_ 4方法。

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

您可以为此使用presence_of_element_locatedvisibility_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中,像attributeToBeattributeContainsattributeNotToBeEmpty这样的方法是可用的,但不确定这些方法在Python中是否可用。

Java示例

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

我很可能会使用自定义等待函数,该函数在客户端站点上运行脚本来检查颜色。

类似于:

from selenium.webdriver.support.ui import WebDriverWait
def has_expected_color(driver):
return driver.execute_script("return document.getElementById(‘myid’).style.backgroundColor === ‘0xfff’")
WebDriverWait(driver).until(has_expected_color)

最新更新