gpt4 book ai didi

java - 使用 Selenium WebDriver 进行搜索可以在 google 上运行,但在 yahoo 上失败

转载 作者:行者123 更新时间:2023-12-01 12:25:08 24 4
gpt4 key购买 nike

全部:

我正在使用 Selenium WebDriver 运行一个简单的 Java 应用程序。
我能够成功运行搜索 上http://www.google.com使用 org.openqa.selenium.htmlunit.HtmlUnitDriver

我尝试在 http://www.yahoo.com 上运行相同的搜索词如以下代码摘录所示:

    CharSequence[] searchTerm = { "bbc", "news" };
// 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.yahoo.com");

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

//searchTerm = "bbc news";
// Enter something to search for
element.sendKeys(searchTerm);


// 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.getTitle());

driver.quit();

但是,它给了我以下错误:

Oct 17, 2014 3:18:44 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Cookie rejected [DNR="deleted", version:0, domain:.www.yahoo.com, path:/, expiry:Thu Oct 17 15:18:45 IST 2013] Illegal domain attribute "www.yahoo.com". Domain of origin: "in.yahoo.com"
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:58'
System info: host: , ip: , os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_05'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:1001)

为什么它对 http://www.google.com 工作得很好但 http://www.yahoo.com 失败?

为什么它会抛出“Exception in thread main org.openqa.selenium.NoSuchElementException Unable tolocate element with name q”错误?

更新答案

感谢 @Sriram 和 @ivan_ochc ,我能够运行以下代码来搜索 http://www.yahoo.com正确地

    // 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.yahoo.com");

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

最佳答案

http://www.yahoo.com没有 name="q"的元素,它有 name="p"的元素

关于java - 使用 Selenium WebDriver 进行搜索可以在 google 上运行,但在 yahoo 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26422720/

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