[英]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: 使用的罐子:
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.