TDD Project/Selenium Web Driver
Selenium Webdriver - iframe 사용하기
솔웅
2013. 12. 5. 11:00
반응형
Handling iFrames using WebDriver
inline frame은 현재의 HTML 문서 내에 다른 문서를 넣을 때 사용됩니다. 즉 iframe은 웹페이지 안에 있는 웹페이지를 말하는거죠. iframe은 별도로 DOM을 가지고 있습니다.
iframe을 사용하는 HTML 소스코드는 아래와 같이 사용하시면 됩니다
위의 코드를 보면 iframe안에 또 다른 iframe이 있는 걸 보실 수 있을 겁니다. 안쪽의 iframe으로 가려면 바깥쪽의 iframe 을 지나가야 되고 또 그 안쪽의 iframe 안에도 body가 있습니다.
그리고 나서 다시 안쪽의 iframe은 을 나와서 바깥쪽 iframe 까지 나오면 이제 OK 버튼을 보게 됩니다. 테스트를 작성하려면 이 과정을 거쳐야 합니다. iframe 안에 있는 element에 직접 접근하는 것은 가능하지 않습니다. iframe은 자신만의 DOM element를 가지고 있어서 그 안의 element들을 다루려면 switch to 를 해서 접근해야 합니다.
Selects the frame 1
driver.switchTo().frame("frame1");
Selects the frame2
driver.switchTo().frame("frame2");
Switching back to the parent window
driver.switchTo().defaultContent(); // 이제 iframe 바깥으로 완전히 빠져 나온 겁니다.
iframe의 value를 알지 못할 때도 있는데요. 그럴 때는 아래와 같이 tagName 메소드를 사요ㅇ해서 그 name을 얻을 수 있습니다.
driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0));
아래 메소드를 사용해서 iframe을 선택하실 수 있습니다.
- frame(index)
- frame(Name of Frame [or] Id of the frame
- frame(WebElement frameElement)
- defaultContent()
반응형