HtmlUnitDriver does not appear to be loading page

穿精又带淫゛_ 提交于 2019-12-01 18:15:55

In the example code given, it assumes the older Google page exists which has the search field with name=q.

The page no longer is labelled that way - try changing driver.findElement(By.name("q")) to driver.findElement(By.cssSelector("input[id=gbqfq]") and see if that fixes your issue - at the very least it should be able to input to the search bar once you get the page loaded up.

If you called driver.getCurrentUrl() immediately after trying to get the page, it may not have loaded completely and instead returned about:blank.

If that doesn't work, we can keep troubleshooting - it's harder to see what's actually happening with a headless browser, so you may want to switch to a FirefoxDriver temporarily to visualize exactly what's happening.

Ill try to give it a shot:

If it is a network problem, please download this tool to check if the port your are connecting to is in use: http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx

Very handy for these kind of situations.

For further debugging, try reading out the source. Put this line before the first element selector and after the page loaded.:

System.out.println(driver.getPageSource());

I wonder if the element is there..

Try using the Selenium IDE plugin with Firefox to see what it scrapes when it does it actions. You can export the code into java and then try to run that also. I would also step through the code on the both home and work computer to see if there are any differences. I have seen code work (when that element not found error happens) when being stepped through due to the DOM having time to fully load. Furthermore, try to instantiate and actual browser so you can visuallly see whats happening.

Also, verify version of all software your using between home and workstation.

1)Try to add

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

before

final WebElement element = driver.findElement(By.name("q"));

2)or try

 try {
      WebElement element = driver.findElement(By.name("q"));
    } catch (Exception e) {
      Thread.sleep(1000);  
    }

If you require headless selenium, and firefox works with your test, try using the phantomjs webdriver.

NoviceTechie

Try using

HtmlUnitDriver driver = new HtmlUnitDriver();

Can you check if the page is loaded by checking

String pageTitle = driver.getTitle();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!