ウェブサイト"qtpselenium.com"にログインするには、以下のコードを書いています。Thread.sleepなしでセレンコードを書く必要があります
コード実行を一時停止するために、間にThread.sleepを指定すると、以下のコードはうまく動作します。 私がThread.sleepにコメントすると、コードが期待通りに機能しません。 要素が表示されるのを待つドライバを作るためにセレンの暗黙的および明示的な待機を使用しようとしましたが、コードはThread.sleepを使用するときにのみ期待通りに機能します。
Thraed.Sleepステートメントを使用せずに以下のコードを動作させる方法はありますか?
セレンコードでThread.sleepステートメントを使用することは悪い習慣ですか?
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
public class QTPSelenium {
public static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver","C:\\Eclipse\\Drivers\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://qtpselenium.com/");
driver.findElement(By.xpath(".//*[@class='btn btn-default member_login']")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("(//button[@type='submit'])[3]")).click();
Thread.sleep(10000);
driver.findElement(By.id("email")).sendKeys("Some Email ID");
driver.findElement(By.id("login-password")).sendKeys("Some Password");
driver.findElement(By.xpath("html/body/main/div[2]/div/div/div[1]/div/div/div/form/button")).click();
}
}
を私はスリープの両方を取り出し、それがまったく待機して私のためにうまく働きました。私は、Chromeのドライバを使用しています... – JeffC
[QMetry Automation Framework](https://qmetry.github.io/qaf/)の使用を開始することができます。内部的にこのような状況を自動的に処理する拡張webdriverとwebelementを提供します。また、明示的な待機条件[アサーション、検証](https://qmetry.github.io/qaf/latest/assertion_verification.html)が要素とともに利用可能です。たとえば、 'element.waitForPresent()'や 'element.assertPresent()'や 'element.verifyPresent()'などです。 – user861594