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

时间:2021-12-10 01:27:24

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

I have a problem with iframe tab. My web page have an iframe in the separate tab which is open using 3rd party link. I want to catch that like by go every tab but not getting any way to do.

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

HTML CODE :

HTML代码:

<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>

Any type of help will be appreciated

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

Thanks @anshul

I run your code but its print same url 4 times.I want to take iframe url and

我运行你的代码,但它打印相同的网址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 个解决方案

#1


1  

You can try something like this :

你可以尝试这样的事情:

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
}

Explanation : 1. Firstly, I am location it from the parent div element which findElement(By.id("horizontalTabAffiliate")) this will locate the outer div using it's id.

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

  1. Once the parenet div is located, you can start location the inner elements in your case innerElement is an "ul", as per the DOM there is only one ul present.findElement(By.xpath("//ul[contains(@class,'resp-tabs-list')]")) this will locate the ul element with the help of the parent div.

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

  2. Once you the ul located, you can get a list of the innermost "li" element. findElements(By.tagName("li")) , this will return a list of the li elements. So you will be having all the tabs located on the above panel which are your third party links.

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

    1. Once these elements are located you can easily perform operation based on that. For your case if you are only interested on clicking the first element of "li", do simply webs.click(); in your for loop().
    2. 找到这些元素后,您可以轻松地执行操作。对于您的情况,如果您只对单击“li”的第一个元素感兴趣,请执行webs.click();在你的for循环()中。

Note you can do much more with the li elements list once it is located. Also make sure the name of class, id's, tagname which I have used above are correct and matching with your DOM.

请注意,一旦找到li元素列表,您就可以执行更多操作。还要确保我在上面使用的class,id,tagname的名称是正确的并且与DOM匹配。

Let me know if this works, or you need more details.

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

#1


1  

You can try something like this :

你可以尝试这样的事情:

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
}

Explanation : 1. Firstly, I am location it from the parent div element which findElement(By.id("horizontalTabAffiliate")) this will locate the outer div using it's id.

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

  1. Once the parenet div is located, you can start location the inner elements in your case innerElement is an "ul", as per the DOM there is only one ul present.findElement(By.xpath("//ul[contains(@class,'resp-tabs-list')]")) this will locate the ul element with the help of the parent div.

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

  2. Once you the ul located, you can get a list of the innermost "li" element. findElements(By.tagName("li")) , this will return a list of the li elements. So you will be having all the tabs located on the above panel which are your third party links.

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

    1. Once these elements are located you can easily perform operation based on that. For your case if you are only interested on clicking the first element of "li", do simply webs.click(); in your for loop().
    2. 找到这些元素后,您可以轻松地执行操作。对于您的情况,如果您只对单击“li”的第一个元素感兴趣,请执行webs.click();在你的for循环()中。

Note you can do much more with the li elements list once it is located. Also make sure the name of class, id's, tagname which I have used above are correct and matching with your DOM.

请注意,一旦找到li元素列表,您就可以执行更多操作。还要确保我在上面使用的class,id,tagname的名称是正确的并且与DOM匹配。

Let me know if this works, or you need more details.

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