下面是为http://www.royalmailgroup.com/主页编写的类
我试图单击“关于我们”链接和FOI联系人。

package sample.keyword;

import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.events.internal.EventFiringMouse;


 public class FeeToPay {

    public static WebElement Menu, SubMenu ;
    public static InternetExplorerDriver driver;
    //public static FirefoxDriver driver;
    public static EventFiringWebDriver eDriver;
    public static EventFiringMouse eMouse;
    public static String xpathMainMenu ="//div[@class='content']/ul/li/span/*";


public void OpenApplication(String Url) throws Exception{

            File file = new File("D:\\Software\\IEDriverServer.exe");
            System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
            driver= new InternetExplorerDriver();

            //driver = new FirefoxDriver();
            eDriver= new EventFiringWebDriver(driver);
            eMouse= new EventFiringMouse(eDriver, null);
            driver.manage().window().maximize();
        try{
            String baseUrl = "http://www.royalmailgroup.com/";
            this.driver.get(baseUrl);

        }
        catch(Exception E){
                throw E;
        }
    }
    public static EventFiringWebDriver getWebDriver(){
        return eDriver;
    }
    public void NavigateTo(String strMenuPath) throws Exception {
    if(strMenuPath == null || strMenuPath.isEmpty())throw new Exception("no menu path mentioned");

    String [] MenuItems = strMenuPath.split("->");
    java.util.List<WebElement> liMenuItems;
    liMenuItems= FeeToPay.getWebDriver().findElements(By.xpath(xpathMainMenu));


    for (int counter =0; counter<MenuItems.length;counter++ ){

        if(counter==0){
            if(liMenuItems.get(counter).getText().equalsIgnoreCase(MenuItems[counter])){
                Locatable item = (Locatable)liMenuItems.get(counter);
                Coordinates c =  item.getCoordinates();
                eMouse.mouseMove(c);
                                }

       }
        if(counter!=0 && counter == MenuItems.length-1 ){
            eDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                WebElement ele = eDriver.findElement(By.linkText(MenuItems[counter]));
                ele.click();
        }
     }


}
}

我低于错误
[VerboseTestNG]失败:“ sample.keyword.FeeToPayTest”-sample.keyword.FeeToPayTest.testOpenApplication()在9033毫秒内完成
[VerboseTestNG] org.openqa.selenium.ElementNotVisibleException:驱动程序试图单击该元素的位置未滚动到视口中。 (警告:服务器未提供任何堆栈跟踪信息)
[VerboseTestNG]命令持续时间或超时:1.93秒

我在用
Selenium Jar Build信息:版本:“ 2.28.0”,修订版:“ 18309”,
系统信息:操作系统名称:Windows 7,操作系统arch:amd64,操作系统版本:6.1,java版本:1.6.0_37

最佳答案

需要添加
WebDriverWait等待=新的WebDriverWait(eDriver,10);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText(MenuItems [counter])));;
在点击子菜单之前。

10-04 17:21