Handling Calendar using Selenium

Dahun Yoo·2020년 5월 24일

How to select specific date or month in Calendar

calendar의 element들은 대부분 같은 클래스를 가지고 있습니다. 때문에 특정 element를 파악하기란 쉽지는 않습니다...
가장 접근하기 쉬운 방법은,
거기서 equal() 을 사용하여 얻고자하는 날짜를 가진 요소를 선택하는 것입니다.

List<WebElement> elements = driver.findElements(By.className(“day”));

for (WebElement element : elements) {
    if(element.getText().equals("31")) {
        element.click();
        break;
    }
}

Generic method to handling month and date

확인하고자 하는 month의 텍스트 정보를 selenium에 알려주고, 해당 텍스트 정보가 나올때까지 캘린더를 클릭하는 그러한 방식으로 접근합니다.

// June 24, 2020 select month and date
WebElement month = driver.findElement(By.xpath(/html/body/div[4]/div[1]/table/thead/tr[1]/th[2]"));
while(!month.getText().contains("June")) {
    Thread.sleep(1000);
    driver.findElement(By.xpath("/html/body/div[4]/div[1]/table/thead/tr[1]/th[3]")).click();
}

List<WebElement> elements = driver.findElements(By.className("day"));

for (WebElement element : elements) {
    if(element.getText().equals("24")) {
        element.click();
        break;
    }
}

위 코드 첫 번째 while 에서, June 문자열을 찾지못하면 다음 달을 선택하도록 되어있음.

원하는 month를 찾게된다면, 그 이후 다시 날짜를 찾기 위해 반복문을 실행하여 계속해서 클릭해주는, 어떻게보면 단순한 방법이라 할 수 있겠습니다.


예를 들어서 특정 날짜 이전의 데이터를 조회하고 싶다! 라고 할 때는 기간의 계산등이 필요할 것 입니다.
날짜 계산방법은 Java의 Date class를 이용하는 건데,

아래의 링크 글에 설명이 잘 되어있기에 링크로 대신 남깁니다.

https://coronasdk.tistory.com/766

profile
QA Engineer

0개의 댓글