以下是爲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