본문 바로가기

트러블 슈팅

Selenium 엘리먼트 선택 과정에서 선택이 되지 않을때

  • 문제점

XPath로 엘리먼트를 찾았는데 엘리먼트를 selenium라이브러리가 인식하지 못하였음

  • 원인

엘리먼트가 inputFrame으로 묶여있었기 때문.

기존 코드:

WebDriverWait wait = new WebDriverWait(driver, 5);

        //5초 기다리고 찾기
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"txt_simple_address\"]")));
        WebElement address = driver.findElement(By.xpath("//*[@id=\"txt_simple_address\"]"));
        executor.executeScript("\"arguments[0].value = '서울시'\"", address);
  • 해결

driverWait을 시킬때, 스위치가 가능 하면 inputFrame으로 driver를 스위치 하도록 코드를 짬.

WebDriverWait wait = new WebDriverWait(driver, 5);

        //5초 기다리고 찾기
        wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("inputFrame")));
        WebElement driverWait = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"txt_simple_address\"]")));
        executor.executeScript("arguments[0].value = '서울시'", driverWait);