使用 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使用哪種方法。
任何建議都會有所幫助!謝謝!
uj5u.com熱心網友回復:
我很可能會使用在客戶端站點上運行腳本的自定義等待函式來檢查顏色。
就像是:
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)
uj5u.com熱心網友回復:
您可以為此使用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和methods 這樣的attributeNotToBeEmpty方法可用,但不確定這些方法在 Python 中是否可用。
Java示例:
WebElement element = driver.findElement(By.xpath("xPath "));
wait.until(ExpectedConditions.attributeToBe(element, "id", "rgb(1, 2, 3)"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/350542.html
標籤:Python 蟒蛇-3.x 硒 硒网络驱动程序 硒铬驱动器
