Selenium Catch例外

时间:2017-09-20 09:46:07

标签: java selenium selenium-webdriver

我正在进行硒测试。对于某些用户,您没有相同数量的页面。这就是为什么我需要在我的代码中实现try catch,但遗憾的是每次他尝试并且元素不存在时他都会停止程序。当我把这个尝试捕获时给出NoSuchElementException。 我尝试在每次点击前放置,如果之前显示的元素,但显示相同的错误。

如果有人可以帮助我那将是非常好的!

try {
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//input[@type='radio' and @value='true']")).click();
        sleep();

        driver.findElement(By.xpath("//input[@type='submit' and @value='>> valider <<']")).click();
        sleep();
 } catch (org.openqa.selenium.NoSuchElementException e) {
            e.printStackTrace();
 }


Log error :

    Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 6496
Only local connections are allowed.
sept. 20, 2017 11:30:38 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFOS: Detected dialect: OSS
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='radio' and @value='true']"}
  (Session info: chrome=60.0.3112.113)
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 22 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'

Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8), userDataDir=C:\Temp\scoped_dir9468_31433}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=60.0.3112.113, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: 3592b946923cfbb74e8e512fb3a212ef
*** Element info: {Using=xpath, value=//input[@type='radio' and @value='true']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:509)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
    at serialys.Test.test(Test.java:83)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

2 个答案:

答案 0 :(得分:0)

您需要首先检查元素是否存在,然后单击该元素,您可以创建以下方法:

public boolean isElementPresent(WebDriver driver, By elem) {
        try {
            driver.findElement(elem);
            return true;
        }catch(NoSuchElementException e) {
            return false;
        }
    }

如果元素存在,此方法返回'true'。

然后,您可以在单击元素之前调用此方法以检查元素是否存在,如下所示:

try {
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        if(isElementPresent(driver, By.xpath("//input[@type='radio' and @value='true']")){
            driver.findElement(By.xpath("//input[@type='radio' and @value='true']")).click();
        }
        sleep();

        driver.findElement(By.xpath("//input[@type='submit' and @value='>> valider <<']")).click();
        sleep();
 } catch (org.openqa.selenium.NoSuchElementException e) {
            e.printStackTrace();
 }

答案 1 :(得分:0)

您正在catch块中打印e.printStackTrace();,并使用您的代码Stacktrace或某些代码

打印System.out.println("Element is not found");
try {
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//input[@type='radio' and @value='true']")).click();
        sleep();

        driver.findElement(By.xpath("//input[@type='submit' and @value='>> valider <<']")).click();
        sleep();
 } catch (org.openqa.selenium.NoSuchElementException e) {
           System.out.println("Element is not found");
 }