调用时,两种方法中的Selenium Webelement失败

时间:2014-02-21 05:40:35

标签: java selenium

我有一个包含可重用方法的密钥类。我试图在两种方法中使用WebElement。他们的关心紧密相连。我知道这是我们失败的因素,因为我可以在不使用两种方法的情况下对其进行编码而且工作正常。当调用包含webelement的方法时,如何在没有失败的情况下正确执行此操作? (没有这样的元素例外)

公共类密钥{

WebElement webElement = null;

public void hoover(String path) throws InterruptedException{

    webElement= driver.findElement(By.xpath(path));
    Actions act = new Actions(driver);
    act.moveToElement(webElement).perform();
    Thread.sleep(5000); 
}


public void click(String path) throws InterruptedException{

    webElement = driver.findElement(By.xpath(path));
    webElement.click();//This opens menu list
    Thread.sleep(5000);
}

另一个类使用如下:

    session.navigate("url");
session.hoover("someTab");
Assert.assertTrue(session.isElementPresent("otherSub"), "Element not present");
session.click("otherSub");

    org.openqa.selenium.NoSuchElementException: no such element
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
a openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:404)
at org.openqa.selenium.By$ByXPath.findElement(By.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
at com.salesforce.util.Keywords.actionHoover(Keywords.java:101)
at com.salesforce.testcases.SmokeTest.topMenuTest(SmokeTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

2 个答案:

答案 0 :(得分:0)

问题可能在于您不等待元素可见。这可能是因为您在可用之前(在将其加载到页面之前)请求它。 Selenium充当用户,因此您需要告诉它等待元素可见然后获取它,而不是立即获取它

public void clickElementWhenReady(By locator, int timeout) {
    WebDriverWait wait = new WebDriverWait(driver, timeout);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(locator));
    element.click();
}

你可以实现类似的东西,以便在一段时间内为元素汇集并在之后抛出错误

答案 1 :(得分:0)

缺少OR.getProperty()

webElement = driver.findElement(By.xpath(OR.getProperty(path)));
webElement.click();//This opens menu list
Thread.sleep(5000);