728x90
반응형

html에 존재하지 않는 element 가져오려고 하면 아래와 같은 에러가 발생

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".protected_form"}

(Session info: chrome=114.0.5735.110)

For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element

Build info: version: '4.9.0', revision: 'd7057100a6'

System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.9'

Driver info: org.openqa.selenium.chrome.ChromeDriver

Command: [8f5b9dd78e65f866dfdd75f4f10b191a, findElement {using=class name, value=protected_form}]

Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 114.0.5735.110, chrome: {chromedriverVersion: 114.0.5735.90 (386bc09e8f4f..., userDataDir: C:\Users\gyu20\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:58243}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:58243/devtoo..., se:cdpVersion: 114.0.5735.110, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}

Session ID: 8f5b9dd78e65f866dfdd75f4f10b191a

at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)

at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)

at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)

at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)

at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)

at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)

at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:193)

at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:183)

at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:158)

at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)

at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:162)

at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:60)

at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)

at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)

at tistoryMavenProject.tistoryAuto.visitTistory(tistoryAuto.java:226)

at tistoryMavenProject.tistoryAuto.main(tistoryAuto.java:303)

 
 

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ElementCheckExample {
    public static void main(String[] args) {
        // WebDriver 경로에 맞게 설정
        System.setProperty("webdriver.chrome.driver", "경로/chromedriver");

        WebDriver driver = new ChromeDriver();
        driver.get("웹페이지 주소");

        // 존재 여부를 확인하고자 하는 요소의 선택자 지정
        By selector = By.id("elementId");

        // 해당하는 모든 요소를 찾음
        // 만약 요소가 존재하지 않으면 빈 리스트를 반환
        // 존재하는 경우 리스트의 크기가 0보다 큼
        // 존재하지 않으면 크기가 0인 리스트
        if (driver.findElements(selector).size() != 0) {
            System.out.println("요소가 존재합니다.");
        } else {
            System.out.println("요소가 존재하지 않습니다.");
        }

        driver.quit();
    }
}

 
위와 같은 예시로 요소 존재 여부 확인 선수행 필요
 

 
위와 같은 소스를 통해 element 존재여부 먼저 파악한 후 다음 프로세스 진행

728x90
반응형

BELATED ARTICLES

more