简体   繁体   中英

Having problems in Selenium example code java

First Error Screen

Second Error Screen

I am running the selenium example code:

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

public class HelloSelenium {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        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("h3>div")));
            System.out.println(firstResult.getAttribute("textContent"));
        } finally {
            driver.quit();
        }
    }
}

And getting the errors as shown in the screenshots above.

Note that the action is being performed but the last statement in the try block isn't printing the attribute of the firstElement. I understand the problem is not very easy to read but solving should be interesting.

Also I am using the geckodriver (for Firefox) in Manjaro.

And I am using gradle.

It states the error on the debug window.

 WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3>div")));

An exception is thrown because there is a timeout on the "wait.until" function.

It doesn't find the element you are searching for.

Your css selector is invalid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM