为什么这段代码在 HtmlUnitDriver 中产生"失败",而在 FirefoxDriver 中产生"成功"?它尝试捕获在页面其余部分几秒钟后加载的文本。我需要HtmlUnitDriver的"成功"。
// WebDriver driver = new HtmlUnitDriver();
WebDriver driver = new FirefoxDriver();
driver.get("https://www.zoro.com/i/G1237047/");
for (int i = 0; i < 360; i++)
if (driver.getPageSource().contains("<span id="availability")) {
Thread.sleep(500);
break;
}
Thread.sleep(500);
if (driver.findElement(By.id("price-box-ships-from-zoro")).getText().contains("1 business day"))
System.out.println("Success");
else
System.out.println("Failure");
尝试以下代码:
for (int i = 0; i < 360; i++)
if (driver.getPageSource().contains("<span id="availability")) {
Thread.sleep(500);
break;
}
String e = driver.findElement(By.xpath("//strong[contains(text(),'1 business day')]")).getText();
System.out.println(e);
Thread.sleep(500);
if (e.contains("1 business day"))
System.out.println("Success");
else
System.out.println("Failure");
也单独获取它们:尝试以下
第一行:
String a = driver.findElement(By.xpath("//div[@id='ships-from-lead-time-G1237047'][@class='ships-from-lead-time']")).getText();
String b = driver.findElement(By.xpath(".//strong[contains(text(),'1 business day.')]")).getText();
System.out.println(a);
System.out.println(b);
第二行:
String c =driver.findElement(By.xpath("//span[@id='price-box-ships-free']")).getText();
System.out.println(c);
第三行:
String d = driver.findElement(By.xpath("//span[@id='price-box-standard-ground']")).getText();
System.out.println(d);