简体   繁体   English

IllegalStateException:在getTitle()时,无法使用HtmlUnitDriver通过com.gargoylesoftware.htmlunit.UnexpectedPage的名称来查找元素

[英]IllegalStateException: Unable to locate element by name for com.gargoylesoftware.htmlunit.UnexpectedPage with HtmlUnitDriver while getTitle()

Here is my basic code for launching HTMLUnit browser and getting the title. 这是我启动HTMLUnit浏览器并获取标题的基本代码。 while running the code i am getting the title as null and later it is throwing the following execpetion: 在运行代码时,我得到的标题为null,后来它抛出以下执行:

Jars used: 使用的罐子:

  • htmlunit-driver-2.33.0-jar-with-dependencies.jar 的HtmlUnit驱动-2.33.0-JAR与 - dependencies.jar
  • selenium-server-standalone-3.14.0.jar 硒的服务器独立,3.14.0.jar

Code trials: 代码试用:

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

public class HtmlUnitDriverTest {

    public static void main(String[] args) throws InterruptedException {

        WebDriver driver = new HtmlUnitDriver();
        Thread.sleep(5000);
        driver.get("https://google.com");
        System.out.println(driver.getTitle());

        driver.findElement(By.name("q")).sendKeys("testing");

    }

}

O/p: O / P:

Exception in thread "main" java.lang.IllegalStateException: Unable to locate element by name for com.gargoylesoftware.htmlunit.UnexpectedPage@2e32ccc5 线程“主”中的异常java.lang.IllegalStateException:无法按名称查找com.gargoylesoftware.htmlunit.UnexpectedPage@2e32ccc5的元素

at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:1285) 在org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:1285)

You need to consider a couple of facts: 您需要考虑几个事实:

  • Inducing Thread.sleep(5000); 诱导Thread.sleep(5000); just after invoking the HtmlUnitDriver() actually makes no sense. 只是在调用HtmlUnitDriver()实际上没有任何意义。 You could have used the Thread.sleep(5000); 您本可以使用Thread.sleep(5000); after you have invoked driver.get() . 调用driver.get() However as per best practices hard-coded Thread.sleep(n) and Implicit Waits should be replaced by the WebDriverWait . 但是,按照最佳实践 ,应将硬编码的Thread.sleep(n)隐式等待替换为WebDriverWait

  • Here you can find a detailed discussion on Replace implicit wait with explicit wait (selenium webdriver & java) 在这里,您可以找到有关用显式等待替换隐式等待的详细讨论(Selenium Webdriver和Java)

  • You are seeing the title as null as the driver is trying to extract the Page Title even before the Page Title is rendered within the HTML DOM 您所看到的标题 null作为驱动程序试图提取甚至在页面标题是内呈现的页面标题 HTML DOM

  • As a solution using htmlunit-driver-2.33.0-jar-with-dependencies.jar you need to induce WebDriverWait for the Page Title to contain a character sequence before extracting and you can use the following solution: 作为使用htmlunit-driver-2.33.0-jar-with-dependencies.jar的解决方案,您需要在提取之前让WebDriverWait页面标题 包含字符序列 ,并且可以使用以下解决方案:

  • Code Block: 代码块:

     import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class A_HtmlunitDriver_2_33_0 { public static void main(String[] args) throws InterruptedException { WebDriver driver = new HtmlUnitDriver(); driver.get("https://google.com"); new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("Go")); System.out.println(driver.getTitle()); } } 
  • Console Output: 控制台输出:

     Google 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Selenium.htmlunit.HtmlUnitDriver 上找到 xpath - Unable to locate xpath on Selenium.htmlunit.HtmlUnitDriver org.openqa.selenium.WebDriverException:com.gargoylesoftware.htmlunit.ScriptException:使用HtmlUnitDriver和Selenium调用getOffsetTop的异常 - org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: Exception invoking getOffsetTop with HtmlUnitDriver & Selenium HtmlUnit com.gargoylesoftware.htmlunit.DefaultCssErrorHandler错误 - HtmlUnit com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[form] attributeName=[name] attributeValue=[docSearch] - com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[form] attributeName=[name] attributeValue=[docSearch] 如何仅从根元素中提取文本内容 - java, com.gargoylesoftware.htmlunit.html - How can I extract text content only from root element - java, com.gargoylesoftware.htmlunit.html Selenium-com.gargoylesoftware.htmlunit.ScriptException:调用jsxSet_innerHTML的异常 - Selenium - com.gargoylesoftware.htmlunit.ScriptException: Exception invoking jsxSet_innerHTML com.gargoylesoftware.htmlunit.ScriptException:错误:Bootstrap 的 JavaScript 需要 jQuery 1.9.1 或更高版本 - com.gargoylesoftware.htmlunit.ScriptException: Error: Bootstrap's JavaScript requires jQuery version 1.9.1 or higher HtmlUnit:下载文件后获取UnexpectedPage - HtmlUnit: Get UnexpectedPage after downloading a file HtmlUnit 找不到元素 - HtmlUnit unable to find element 无法找到元素:{“方法”:“名称”,“选择器”:“ markUp” - Unable to locate element: {“method”:“name”,“selector”:“markUp”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM