2017-10-26 9 views
0

私はSELENIUMを初めて使用しています。 私はページを開いていくつかの要素がページ上に存在するテストを書いた。 要素が存在しない場合、try/catchインターセプトエラーメッセージが表示され、コンソール通知に出力されます。私は肯定的な結果(要素が存在する)を持っている間、TRYブロックへのコードがうまくいきます。しかし、私が否定的な結果をもたらし、要素が構造を見つけられなかった場合、CATCHは機能しません。どうか、私に説明してください。構造 "try/catch"は例外をキャッチしません "NoSuchElementException"

package ua.com.local; 

import org.openqa.selenium.By; 
import org.openqa.selenium.NoSuchElementException; 
import org.openqa.selenium.WebElement; 
import org.testng.Assert; 
import org.testng.annotations.Test; 

public void OpenPage() { 
    driver.findElement(By.xpath("//button[@class='filter-submit__btn']")).click(); 
      Assert.assertTrue(isElementPresent(driver.findElement(By.xpath("//form[@class='section-filter__form']")))); 
      driver.quit(); 
     } 

    public boolean isElementPresent(WebElement element) { 
     try { 
      element.isEnabled(); 
      System.out.println("Element found!!! " + element); 
      return true; 
     } catch (org.openqa.selenium.NoSuchElementException e) { 
      System.out.println("NoSuchElementException!!"); 
      return false; 
     } 
    } 

コンソール出力

Starting ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a) on port 5367 
Only local connections are allowed. 
окт 25, 2017 3:57:38 PM org.openqa.selenium.remote.ProtocolHandshake createSession 
INFO: Detected dialect: OSS 
Element found!!! [[ChromeDriver: chrome on XP (be9a3b360b5258e093a80c82282b0a23)] -> xpath: //a[@href='/category/dulkiu-siurbliai/']] 
Element found!!! [[ChromeDriver: chrome on XP (be9a3b360b5258e093a80c82282b0a23)] -> xpath: //a[@href='/category/sulankstomos-dziovykles/']] 
Element found!!! [[ChromeDriver: chrome on XP (be9a3b360b5258e093a80c82282b0a23)] -> xpath: //a[@href='/category/indaploves/']] 

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//form[@class='section-filter__form']"} 
    (Session info: chrome=61.0.3163.100) 
    (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information) 
Command duration or timeout: 0 milliseconds 
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html 
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T15:28:36.4Z' 
System info: host: 'AMELNYCHENKO', ip: '192.168.110.2', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_144' 
Driver info: org.openqa.selenium.chrome.ChromeDriver 
Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false, platform=XP, acceptSslCerts=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=XP, setWindowRect=true, unexpectedAlertBehaviour=, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a), userDataDir=C:\Users\Andrey\AppData\Local\Temp\scoped_dir5444_8929}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, version=61.0.3163.100, browserConnectionEnabled=false, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}] 
Session ID: be9a3b360b5258e093a80c82282b0a23 
*** Element info: {Using=xpath, value=//form[@class='section-filter__form']} 

    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:214) 
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166) 
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40) 
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82) 
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45) 
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) 
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) 
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:356) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:458) 
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348) 
    at ua.com.local.ZudMainPageTest.OpenPage(ZudMainPageTest.java:24) 
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) 
    at org.testng.TestRunner.privateRun(TestRunner.java:744) 
    at org.testng.TestRunner.run(TestRunner.java:602) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:289) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) 
    at org.testng.TestNG.runSuites(TestNG.java:1144) 
    at org.testng.TestNG.run(TestNG.java:1115) 
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) 
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123) 


=============================================== 
ZudSuite1 
Total tests run: 1, Failures: 1, Skips: 0 
=============================================== 
+2

あなたはスタックトレースを読めば、例外が「driver.findElement(.....」 –

答えて

1

は理由は、例外がスローされたライン

driver.findElement(By.xpath("//form[@class='section-filter__form']")); 

はあなたtryブロック内ではありません。

public boolean isElementPresent(By identifier) { 
    try { 
     element = driver.findElement(identifier); 
    } catch (org.openqa.selenium.NoSuchElementException e) { 
     System.out.println("NoSuchElementException!!"); 
     return false; 
    } 

    element.isEnabled(); 
    System.out.println("Element found!!! " + element); 
    return true; 
} 

このように確認できます。

Assert.assertTrue(isElementPresent(By.xpath("//form[@class='section-filter__form']")); 
+0

**アレクセイR。**、あなたがで何を意味する「'(内部で発生していることがわかりますでしょう) '? – Andrew

+0

' webelement'のオブジェクトを渡すのではなく、 '' try' *。 – EJP

+0

@Andrewの中にいないと、 ''Enter'メソッドの入力である' By'のオブジェクトを渡して新しい ' webelement'。この方法では、メソッド内に新しいweb要素を作成しています。 –

関連する問題