.switchTo().frame(<'frameId'>); not wor

2020-07-27 21:08发布

问题:

I am kinda new to HtmlUnit and am having some trouble getting a "Setup" menu item which situated in the frame.
Below code works perfectly fine for FireFox driver while fails for HtmlUnitDriver,

   HtmlUnitDriver driver = new HtmlUnitDriver();
   driver.get(fleetWorkURL);

   WebElement usernameElement = driver.findElement(By.name("j_username"));
   usernameElement.sendKeys(username);

   WebElement passwordElement = driver.findElement(By.name("j_password"));
   passwordElement.sendKeys(password);

   WebElement logInButton = driver.findElement(By.className("button_acunia"));
   logInButton.click();

   driver.switchTo().frame("headerFrame");
   WebElement setupMenuItemElement = driver.findElement(By.id("setup"));
   setupMenuItemElement.click();

   // Check the title of the page, should be Fleet Works Setup
   System.out.println("Page title is: " + driver.getTitle());
   driver.close();

Error while using HtmlUnit Driver -

   Exception in thread "main" org.openqa.selenium.NoSuchFrameException: Unable to locate        frame with name or ID: headerFrame
  Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 15:53:30'
 System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version:  '1.7.0_13'
 Driver info: driver.version: unknown
at org.openqa.selenium.htmlunit.HtmlUnitDriver$HtmlUnitTargetLocator.frame(HtmlUnitDriver.java:908)
at com.yukon.qasetup.xmlService.Example.main(Example.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Frames:

   <frameset border="0" rows="0,0,0,0,0,90,*" onunload="leave()"   onload="initMainWindow()">
<frame src="/fleet/html/empty.html" name="refreshTerminalsFrame" style="visibility: hidden; width: 0px; height: 0px; border: 0px;">
    <frame src="/fleet/html/empty.html" name="refreshDriversFrame" style="visibility: hidden; width: 0px; height: 0px; border: 0px;">
<frame src="/fleet/html/empty.html" name="refreshExternalDevicesFrame" style="visibility: hidden; width: 0px; height: 0px; border: 0px;">
<frame src="/fleet/html/empty.html" name="refreshUsersFrame" style="visibility: hidden; width: 0px; height: 0px; border: 0px;">
<frame src="/fleet/html/empty.html" name="refreshCustomersFrame" style="visibility: hidden; width: 0px; height: 0px; border: 0px;">
    <frame src="/fleet/jsp/header/headerFrame.jsp" name="headerFrame" frameborder="0" scrolling="no">   
    <frame src="/fleet/home.do" name="displayFrame" frameborder="0" scrolling="yes">
     </frameset>

回答1:

Try the following:

HtmlUnitDriver driver = new HtmlUnitDriver(true); // enable javascript