如何在selenium上检测带有原始窗口链接的iframe选项卡窗口

时间:2017-07-27 07:57:03

标签: javascript html selenium iframe webdriver

Ifram

我遇到iframe标签问题。我的网页在单独的标签中有一个iframe,该标签使用第三方链接打开。我想抓住每个标签,但没有任何办法。

HTML CODE:

<ul class="resp-tabs-list">
    <li class="resp-tab-item resp-tab-active" aria-controls="tab_item-0" role="tab">
        <a class="js-tab-list js-tab-1" data-source="https://www.adsdssas.com/property/individual/BC-2030877?isl=1" data-no="1" data-ga="0" target="iframe_1">
            <img src="/themes/centralization/images/partners-tab-logo/rentalhomes-tab-logo.png" alt="RentalHomes.com" height="20" width="auto">
        </a>
    </li>

    <li class="resp-tab-item" aria-controls="tab_item-1" role="tab">
        <a class="js-tab-list js-tab-1" data-source="https://www.jdoasdadadqocy.com/click-8111003-11553823?url=https%3A%2F%2Fwww.vrasbo.com%2Fvacation-rentals%3Fq%3DDauphin%2BIsland%252C%2BAL%252C%2BUSA&amp;utm_campaign=RENTALHOMES&amp;sid=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&amp;label=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&amp;ft=1" data-no="1" data-ga="0" target="iframe_1">
            <img src="/themes/centralization/images/partners-tab-logo/feed_logo_vrbo.png" alt="VRBO.Com" height="20" width="auto">
        </a>
    </li>

    <li class="resp-tab-item" aria-controls="tab_item-2" role="tab">
        <a class="js-tab-list js-tab-1" data-source="https://www.jdadsdoqocy.com/click-8111003-10859031?url=https%3A%2F%2Fwww.homeaway.com%2Fresults%2Fkeywords%3ADauphin%2BIsland%252C%2BAL%252C%2BUSA&amp;utm_campaign=RENTALHOMES&amp;sid=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&amp;label=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&amp;ft=1" data-no="1" data-ga="0" target="iframe_1">
            <img src="/themes/centralization/images/partners-tab-logo/feed_logo_12.png" alt="HomeAway.Com" height="20" width="auto">
        </a>
    </li>

    <li class="resp-tab-item" aria-controls="tab_item-3" role="tab">
        <a class="js-tab-list js-tab-1" data-source="https://www.dddoaoa.com/click-8111003-11327743?url=https://www.asdasd.com/search.do?q-destination=Dauphin+Island%2C+AL%2C+USA&amp;utm_campaign=RENTALHOMES&amp;sid=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&amp;label=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&amp;ft=1" data-no="1" data-ga="0" target="iframe_1">
            <img src="/themes/centralization/images/partners-tab-logo/feed_logo_14.png" alt="Hotel.Com" height="20" width="auto">
        </a>
    </li>
</ul>

任何类型的帮助将不胜感激

Thanks @anshul

我运行你的代码,但它打印相同的网址4次。我想拿iframe网址和

@Test
public void DetailsPageSliderTAB_For_BC() throws InterruptedException{
    wd.manage().window().maximize();
    wd.get("http://192.168.2.116/check/");
    wd.findElement(By.xpath(".//*[@id='Rentalhomes']/div/div/div/table/tbody[4]/tr[6]/td[1]/a")).click();

    wd.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
    String HotelonBCP=wd.findElement(By.xpath(".//*[@class='air-bnb-checkbox checkbox']")).getText();
    wd.findElement(By.xpath(".//*[@id='lightbox-pop']/div/div/div/div[2]/div/div[5]/div[2]/div[2]/div/a")).click();
    wd.findElement(By.xpath(".//*[@id='start_now11']")).click();
    Thread.sleep(200000);
    Reporter.log("POP Under Name "+HotelonBCP);


    for (String windows : wd.getWindowHandles()) {

        wd.switchTo().window(windows);

        if (wd.getCurrentUrl().startsWith("http://staging.rentalhomes.com")) {
            List<WebElement> liElementList = wd.findElement(By.id("horizontalTabAffiliate")).findElement(By.xpath("//ul[contains(@class,'resp-tabs-list')]")).findElements(By.tagName("li"));

             for (WebElement webElement : liElementList) {
             // Based on the webElement perform operation
                 wd.findElement(By.xpath(".//*[@id='horizontalTabAffiliate']/ul/li[1]/a")).click();
                 System.out.println(wd.getCurrentUrl());
            }
        }
        }
    }


 <li class="resp-tab-item" aria-controls="tab_item-1" role="tab">
                        <a class="js-tab-list js-tab-1" data-source="https://www.jdoqocy.com/click-8111003-11553823?url=p;ft=1" data-no="1" data-ga="0" target="iframe_1">
                            <img src="/themes/centralization/images/partners-tab-logo/feed_logo_vrbo.png" alt="VRBO.Com" height="20" width="auto">
                        </a>
                    </li>

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

List<WebElement> liElementList = driver.findElement(By.id("horizontalTabAffiliate")).findElement(By.xpath("//ul[contains(@class,'resp-tabs-list')]")).findElements(By.tagName("li"));

 for (WebElement webElement : liElementList) {
 // Based on the webElement perform operation
}

说明:  1.首先,我是父div元素的位置, findElement(By.id(“horizo​​ntalTabAffiliate”))这将使用它的id找到外部div。

  1. 找到parenet div后,你可以开始定位你的情况中的内部元素innerElement是一个“ul”,根据DOM,只有一个ul存在。 findElement(By.xpath(“// ul [contains(@ class,'resp-tabs-list')]”))这将在父div的帮助下找到ul元素。

  2. 找到ul后,您可以获得最里面的“li”元素的列表。 findElements(By.tagName(“li”)),这将返回li元素的列表。因此,您将拥有位于上方面板上的所有标签,这些标签是您的第三方链接。

    1. 找到这些元素后,您可以轻松地执行操作。对于您的情况,如果您只想点击“li”的第一个元素,只需在for循环中执行 webs.click();
  3. 注意一旦找到了li元素列表,您就可以做更多的事情。还要确保我上面使用的class,id,tagname的名称是正确的并且与您的DOM匹配。

    让我知道这是否有效,或者您需要更多细节。