以下是为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秒
我使用 硒罐生成信息:版本:“2.28.0”,修订版:“18309”, 系统信息:os.name: 'Windows 7的',os.arch: 'AMD64',os.version: '6.1',java.version: '1.6.0_37'
你是否等到组件完全加载到网页? – Smit
第一个if循环for循环将悬停在第一个菜单项'Aboout us'上。第二个循环是单击子菜单项。我不会在这之间等待。 –
我在空闲时间只用了硒。看起来你不是通过硒自动生成测试用例,然后转换为TestNG java代码。但是,您可以使用显式和隐式等待。点击此链接[显式和隐式等待](http://seleniumhq.org/docs/04_webdriver_advanced.jsp) – Smit