First Error Screen

Second Error Screen

我正在运行硒示例代码:

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();
        }
    }
}

并获得上面的屏幕快照中所示的错误。

请注意,该操作正在执行,但try块中的最后一条语句未打印firstElement的属性。我知道问题不是很容易理解,但是解决应该很有趣。

我也在Manjaro中使用geckodriver(对于Firefox)。

我正在使用gradle。

最佳答案

它在调试窗口中指出错误。

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

因为“wait.until”函数超时,所以引发异常。

它找不到您要搜索的元素。

您的CSS选择器无效。

07-25 21:00
查看更多