Selenium WebDriverWait允许更改元素属性

Selenium WebDriverWait允许更改元素属性,selenium,webdriver,selenium-webdriver,Selenium,Webdriver,Selenium Webdriver,如何使用WebDriverWait等待属性更改 在我的AUT中,我必须等待按钮启用后才能继续,不幸的是,由于开发人员编写页面的方式,我无法使用WebElement的isEnabled()方法。开发人员正在使用一些CSS来使按钮看起来像被禁用了一样,这样用户就不能点击它,并且isEnabled方法对我来说总是返回true。所以我要做的是获取属性“aria disabled”,并检查文本是“true”还是“false”。到目前为止,我一直在使用Thread.sleep进行for循环,类似这样: fo

如何使用WebDriverWait等待属性更改

在我的AUT中,我必须等待按钮启用后才能继续,不幸的是,由于开发人员编写页面的方式,我无法使用WebElement的isEnabled()方法。开发人员正在使用一些CSS来使按钮看起来像被禁用了一样,这样用户就不能点击它,并且isEnabled方法对我来说总是返回true。所以我要做的是获取属性“aria disabled”,并检查文本是“true”还是“false”。到目前为止,我一直在使用Thread.sleep进行for循环,类似这样:

for(int i=0; i<6; ++i){
    WebElement button = driver.findElement(By.xpath("xpath"));
    String enabled = button.getText()
    if(enabled.equals("true")){ break; }
    Thread.sleep(10000);
 }
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC    

xp = '//*[@id="element_id"][@aria-disabled="false"]'
WebDriverWait(browser, 60).until(EC.presence_of_element_located((By.XPATH, xp)))

显然,它不起作用,因为函数需要的是WebElement而不是字符串,但这正是我要计算的。有什么想法吗

以下内容可能有助于满足您的需求。 在下面的代码中,我们重写了包含我们正在寻找的条件的apply方法。因此,只要条件不是真的,在我们的例子中,enabled不是真的,我们就进入一个循环,最多10秒,每500毫秒轮询一次(这是默认值),直到apply方法返回真

WebDriverWait wait = new WebDriverWait(driver,10);

wait.until(new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver driver) {
        WebElement button = driver.findElement(By.xpath("xpath"));
        String enabled = button.getAttribute("aria-disabled");
        if(enabled.equals("true")) 
            return true;
        else
            return false;
    }
});
WebDriverWait wait=newwebdriverwait(驱动程序,10);
等待.直到(新的ExpectedCondition(){
公共布尔应用(WebDriver驱动程序){
WebElement button=driver.findElement(By.xpath(“xpath”);
String enabled=button.getAttribute(“aria disabled”);
如果(启用。等于(“真”))
返回true;
其他的
返回false;
}
});

如果有人想在Selenium包装中使用@Sri作为一种方法,这里有一种方法(感谢btw):

public void waitforAttribute已更改(按定位器、字符串属性、字符串初始值){
WebDriverWait wait=新的WebDriverWait(this.driver,5);
等待.until(新的ExpectedCondition(){
通过定位器进行私有化;
私有字符串属性;
私有字符串初始值;
私有ExpectedCondition init(按定位器、字符串属性、字符串初始值){
this.locator=定位器;
this.attr=attr;
this.initialValue=initialValue;
归还这个;
}
公共布尔应用(WebDriver驱动程序){
WebElement按钮=driver.findElement(this.locator);
字符串enabled=button.getAttribute(this.attr);
if(enabled.equals(this.initialValue))
返回false;
其他的
返回true;
}
}.init(定位器、属性、初始值));
}

当我在一个相当慢的引导页面上等待排序按钮生效时,这对我来说很有效

    new WebDriverWait(webDriver, 10)
.until(ExpectedConditions.attributeContains(BUTTON_SORT_ASCENDING_PRICE, "class", "sortButtonActive"));
  • “webDriver”-我当前的webDriver实例
  • “10”-超时秒
  • “按钮\u排序\u升序\u价格”-元素的按定位器
  • “类”-属性
  • “sortButtonActive”-我正在等待的“类”的值

Selenium V3.01

也可以使用简单的do-while循环System.currentTimeMillis()完成;方法可用于避免无限循环

long startTime = System.currentTimeMillis();
String disabled;
do{
   disabled = element.getAttribute("attributeName").trim();
}while(disabled!="false" && System.currentTimeMillis()-startTime<10000);
long startTime=System.currentTimeMillis();
字符串禁用;
做{
disabled=element.getAttribute(“attributeName”).trim();

}当(disabled!=“false”&&System.currentTimeMillis()-startTime时,您可以使用xpath使用所需值的属性定义元素,然后等待它出现在页面上。 在Python中,它可能如下所示:

for(int i=0; i<6; ++i){
    WebElement button = driver.findElement(By.xpath("xpath"));
    String enabled = button.getText()
    if(enabled.equals("true")){ break; }
    Thread.sleep(10000);
 }
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC    

xp = '//*[@id="element_id"][@aria-disabled="false"]'
WebDriverWait(browser, 60).until(EC.presence_of_element_located((By.XPATH, xp)))