有人可以帮我解决以下问题: 有一个iframe并且正确地通过xPath找到它。
div[@id='sidebar']/div[@class='banner-300-250']//iframe
这是源代码:
<div class="banner-300-250">
<script type="text/javascript">if(!window.AdButler){(function(){var s=document.createElement("script");s.async=true;s.type="text/javascript"; s.src='https://servedbyadbutler.com/app.js';var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(s,n);}());}</script>
<script type="text/javascript">var AdButler=AdButler||{}; AdButler.ads=AdButler.ads||[];var abkw=window.abkw||'';var plc177931=window.plc177931||0;document.write('<'+'div id="placement_177931_'+plc177931+'"></'+'div>'); AdButler.ads.push({handler:function(opt){AdButler.register(166212,177931,[300,250],'placement_177931_'+opt.place,opt);},opt:{place:plc177931++,keywords:abkw,domain:'servedbyadbutler.com'}});</script> <script async="" type="text/javascript" src="https://servedbyadbutler.com/adserve/;ID=166212;size=300x250;setID=177931;type=async;domid=placement_177931_0;place=0;pid=894989;sw=1920;sh=1080;spr=1;rnd=894989;kw=sport"></script><div id="placement_177931_0"><iframe id="placement_177931_0_iframe" frameborder="0" scrolling="no" height="250" width="300"></iframe></div>
</div>
<iframe id="placement_177931_0_iframe" frameborder="0" scrolling="no" height="250" width="300"></iframe>
.
.
.
<a href="//wlpinnaclesports.adsrv.eacdn.com/C.ashx?btag=a_773b_3429c_&affid=4609&siteid=773&adid=3429&c=&MediaID=3429&IsAd=1& <script src="//pinnaclesports.host.bannerflow.com/bf_ph_642.js?btag=a_773b_3429" charset="utf-8" type="text/javascript"> </script>
</a>
尝试使用此代码,并且当iframe显示时它可以正常工作,但是在某些页面上它不存在,在这种情况下我正在进行破解测试(在下面的代码中没有添加简单的for循环,但它是写的,因此,在下面的代码中是反击(i):
List<WebElement> bookieBanners = driver.findElements(By.xpath("//div[@id='sidebar']/div[@class='banner-300-250']"));
System.out.println(bookieBanners.);
if (bookieBanners.get(i).isDisplayed()) {
System.out.println(bookieBanners);
bookieBanners.get(i).click();
BaseTest.sleepSeconds(2);
switchToLastOpenedWindow(driver);
System.out.println("Opened is page: " + driver.getCurrentUrl());
BaseTest.closePrevWindows(driver);
} else {
System.out.println("There is no banner for bookies");
continue;
}
提前谢谢。
答案 0 :(得分:0)
经过一番努力,解决方案如下:
List<WebElement> subMenuElements = driver.findElements(By.xpath(Found Elements));
if (subMenuElements.size() > 0) {
for (int p = 0; p < subMenuElements.size(); p++) {
if (p < 9) {
System.out.println("");
System.out.println("******");
//Open in another tab that page
((JavascriptExecutor) driver).executeScript("$('a').attr('target', '_blank');");
subMenuElements.get(p).click();
BaseTest.sleepSeconds(3);
System.out.println("Checking banner on " + subMenuElements.get(p).getText());
System.out.println("Checking banner on " + driver.getCurrentUrl());
List<WebElement> bookieLinks = driver.findElements(By.xpath("Found inner iframe"));
System.out.println("Second banner:");
System.out.println("*****");
if ((bookieLinks.size() > 0)) {
WebElement bookieShown = driver.findElement(By.xpath("Banner is clickable at position 'banner-300-250']"));
bookieShown.click();
switchToLastOpenedWindow(driver);
BaseTest.sleepSeconds(3);
System.out.println(driver.getCurrentUrl());
System.out.println("Page title: " + driver.getTitle());
BaseTest.closePrevWindows(driver);
} else {
System.out.println("Banner not shown on current page");
BaseTest.closePrevWindows(driver);
}