1

Why does this code yield "Failure" with HtmlUnitDriver but "Success" with FirefoxDriver? It is trying to capture text that loads several seconds after the rest of the page. I need "Success" with 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");

1 Answers1

0

Try the following code:

  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");

Also to get them Individually: Try following

First line:

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);

Second Line:

String c =driver.findElement(By.xpath("//span[@id='price-box-ships-free']")).getText();
System.out.println(c);

Third Line:

String d = driver.findElement(By.xpath("//span[@id='price-box-standard-ground']")).getText();
System.out.println(d);
Kishan Patel
  • 1,385
  • 3
  • 13
  • 24
  • That gives the exception below. And what do you mean by "use some wait". Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //strong[contains(text(),'1 business day')] For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00' – user7766601 Mar 26 '17 at 17:23
  • The above edited code works fine for me and I am getting Success in console as well. – Kishan Patel Mar 27 '17 at 04:40
  • The first one, which is the one that matters, comes back with an empty string for success case driver.findElement(By.id("ships-from-lead-time-G1237047")).getText() and failure case driver.findElement(By.id("ships-from-lead-time-G2482308")).getText(). What is the full code you are using? Are you specifying the browser emulation? – user7766601 Mar 27 '17 at 17:42
  • You talking about `(driver.findElement(By.xpath("//strong[contains(text(),'1 business day')]")).getText().contains("1 business day"))` ? – Kishan Patel Mar 27 '17 at 17:44
  • Are you not getting success in console ? I got that – Kishan Patel Mar 27 '17 at 17:44
  • Try the edited one . I have taken them individually now. – Kishan Patel Mar 27 '17 at 17:50
  • I get the error below at the line beginning "String e=" : Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //strong[contains(text(),'1 business day')] ...org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:539) at com.materrollc.TestRun.main(TestRun.java:22) – user7766601 Apr 24 '17 at 03:14
  • I don't think there is problem with code. Which browser you are using ? Try to use chrome and check the same if it is working or not. – Kishan Patel Apr 24 '17 at 04:55
  • I get the same error with FirefoxDriver and HtmlUnitDriver. Are you able to get your code to work with HtmlUnitDriver, with "success" for "G1237047" and "failure" for "G2482308"? Do you agree with the comment above that "With direct HtmlUnit usage, there is an exception which needs JavaScript investigation"? – user7766601 Apr 24 '17 at 15:37