gpt4 book ai didi

java - 如何解决 org.openqa.selenium.NoSuchElementException

转载 作者:搜寻专家 更新时间:2023-11-01 01:38:29 24 4
gpt4 key购买 nike

我正在尝试运行这个程序:

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class HtmlDriver {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();

// And now use this to visit Google
driver.get("http://www.stumbleupon.com/home/");

// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// Enter something to search for
element.sendKeys("Cheese!");

// Now submit the form. WebDriver will find the form for us from the element
element.submit();

// Check the title of the page
System.out.println("Page title is: " + driver.getPageSource());
}
}

我收到以下异常:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q System info: os.name: 'Linux', os.arch: 'i386', os.version: '2.6.27-7-generic', java.version: '1.6.0_12' Driver info: driver.version: HtmlDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:651) at org.openqa.selenium.By$4.findElement(By.java:148) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1133) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:869) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1130) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:330) at com.webdrivertest.HtmlDriver.main(HtmlDriver.java:20)

请帮我解决一下。

最佳答案

该页面上没有 name="q"的元素,因此出现 NoSuchElementException。您从 google 获取示例并更改了它要访问的站点,但它仍在页面上寻找 google 搜索框。

关于java - 如何解决 org.openqa.selenium.NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4601296/

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