我正在尝试与iFrame
中的元素进行交互,实际上是嵌入了YouTube视频。如果我尝试获取iFrames
的大小,我将得到1,但是如果我尝试获取有关其的任何其他信息,我将得到
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document.
或者,如果我想单击获得的元素
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable
这是iframe元素:
<iframe id="am9obi1jb29wZXItd29ya3MvbWFpbi9wYXIvcHJvZHVjdC1lZGl0b3JpYWwvcHJvZHVjdEVkaXRvcmlhbFBhci9lZGl0b3JpYWxfZ3JpZF9yb3dfMTQ2Njc5NTcwMi9sZWZ0UGFyL3ZpZGVvX2l0ZW0veW91dHViZQ" frameborder="0" allowfullscreen="1" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" width="640" height="360" src="https://www.youtube.com/embed/uIbhNKfbico?autoplay=0&cc_load_policy=0&hl=de&rel=0&modestbranding=1&enablejsapi=1&origin=https%3A%2F%2Fm3.mini-int1.com&widgetid=1"></iframe>
这是我想单击的iFrame
内的播放按钮:
<button class="ytp-large-play-button ytp-button" aria-label="Wiedergabe"><svg height="100%" version="1.1" viewBox="0 0 68 48" width="100%"><path class="ytp-large-play-button-bg" d="M66.52,7.74c-0.78-2.93-2.49-5.41-5.42-6.19C55.79,.13,34,0,34,0S12.21,.13,6.9,1.55 C3.97,2.33,2.27,4.81,1.48,7.74C0.06,13.05,0,24,0,24s0.06,10.95,1.48,16.26c0.78,2.93,2.49,5.41,5.42,6.19 C12.21,47.87,34,48,34,48s21.79-0.13,27.1-1.55c2.93-0.78,4.64-3.26,5.42-6.19C67.94,34.95,68,24,68,24S67.94,13.05,66.52,7.74z" fill="#212121" fill-opacity="0.8"></path><path d="M 45,24 27,14 27,34" fill="#fff"></path></svg></button>
我试图通过多种方式切换到框架(通过xpath,通过webElement),但是这些都不起作用
这是我到目前为止尝试过的:
public void clickElementsOnYoutubeVideo() throws Exception {
MiniProductPageSelectors miniProductPageSelectors = new MiniProductPageSelectors();
final List<WebElement> iFrames = DriverUtil.getDriver().findElements(By.tagName("iframe"));
System.out.println(iFrames.size());
Thread.sleep(3000);
System.out.println(DriverUtil.getDriver().getPageSource().contains("iframe"));
for (WebElement iframe : iFrames) {
DriverUtil.getDriver().switchTo().frame(iframe);
System.out.println(iframe.getAttribute("id"));
miniProductPageSelectors.youtubeLargePlayButtonList().click();
}
}