使用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_located
或visibility_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)"));
我很可能会使用自定义等待函数,该函数在客户端站点上运行脚本来检查颜色。
类似于:
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)