0

I just installed the Selenium JAR file, and I copied a small program off the Internet to test if everything was fine. I ran into this issue and the Internet did not give a concrete solution. The following program is what I copied.

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
import java.time.Duration;

public class sample {

    public static void main(String args[]) {
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        try {
            driver.get("https://google.com/ncr");
            driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);
            WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("//a/h3")));
            System.out.println(firstResult.getAttribute("textContent"));
        } finally {
            driver.quit();
        }
    }
}

I get an error saying "the constructor webdriverwait(webdriver duration) is undefined" even though I imported the correct files. How can I fix it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
coder123
  • 1
  • 1

1 Answers1

0

The (or a) problem is that Duration.ofSeconds() returns an object with type Duration, and WebDriverWait is expecting to receive a long in that argument. Try simply

WebDriverWait wait = new WebDriverWait(driver, 10);
C. Peck
  • 3,641
  • 3
  • 19
  • 36
  • What is the underlying reason? [Deprecated constructors](https://stackoverflow.com/questions/58993667/webdriverwait-is-deprecated-in-selenium-4)? (Since it must once have worked in some context.) – Peter Mortensen Oct 31 '22 at 21:53
  • [The official documentation](https://www.selenium.dev/documentation/webdriver/waits/) has an example using *Duration.ofSeconds(10)*: `WebElement firstResult = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//a/h3")));`. Isn't there more to it? – Peter Mortensen Nov 01 '22 at 00:59