gpt4 book ai didi

java - 在 selenium 的每一行中替换 thread.sleep(9000)

转载 作者:行者123 更新时间:2023-11-29 04:45:17 25 4
gpt4 key购买 nike

I have been using thread.sleep(9000) almost after every line of code in selenium which is making me wait for long.Can anybody suggest me an alternate way to reduce this.As my application is taking time load a page it needs to wait until a particular page is loaded to perform any action.

WebElement un = driver.findElement(By.id("login_username_id"));
un.sendKeys(username);
WebElement pwd = driver.findElement(By.id("login_password_id"));
pwd.sendKeys(password);

try {
Thread.sleep(25000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
driver.findElement(By.id("login_submit_id")).click();
try {
Thread.sleep(9000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

我想在每一行之后减少 thread.sleep 的使用,并使用一个通用函数,以便它在需要时等待。

最佳答案

使用下面的例子:

public class Main{

static WebDriver driver = new FirefoxDriver();

public static void main(String [] args) throws InterruptedException{

WebElement un = driver.findElement(By.id("login_username_id"));
un.sendKeys(username);
WebElement pwd = driver.findElement(By.id("login_password_id"));
pwd.sendKeys(password);

waitForElement(By.id("ur id"),60);

driver.findElement(By.id("login_submit_id")).click();

waitForElement(By.id("ur id"),60);


}

/**
* wait until expected element is visible
*
* @param expectedElement element to be expected
* @param timeout Maximum timeout time
*/
public static void waitForElement(By expectedElement, int timeout) {
try {
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.visibilityOfElementLocated(expectedElement));

} catch (Exception e) {
e.printStackTrace();
//System.out.println("print ur message here");
}
}
}

如果你有任何困惑,请告诉我。

关于java - 在 selenium 的每一行中替换 thread.sleep(9000),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37433432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com