【问题标题】:How to detect iframe tab window with original window link on selenium如何在 selenium 上检测带有原始窗口链接的 iframe 选项卡窗口
【发布时间】:2018-01-02 19:42:44
【问题描述】:

我对 iframe 选项卡有疑问。我的网页在使用 3rd 方链接打开的单独选项卡中有一个 iframe。我想通过去每个标签来抓住它,但没有任何办法去做。

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>

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

Thanks @anshul

我运行你的代码,但它打印相同的 url 4 次。我想采用 iframe url 和

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

【问题讨论】:

  • 你能说清楚一点吗,你想得到所有在 iframe 中打开的链接吗?或者你想点击链接然后>移动到新标签作为链接打开然后>找到iframe并使用一些元素执行你的操作?

标签: javascript html selenium iframe webdriver


【解决方案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 loop() 中执行 webs.click();。

注意一旦找到 li 元素列表,您可以做更多事情。还要确保我上面使用的类名、id 和标记名是正确的并且与你的 DOM 匹配。

让我知道这是否可行,或者您需要更多详细信息。

【讨论】:

  • 非常感谢..我编辑我的帖子请检查。这里我想获取当前 URL,我想获取框架数据 URL,如 data source="jdoqocy.com...。请参阅我的 post.below 我在需要的地方添加了 HTML 代码和 iframe 选项卡 URL
猜你喜欢
  • 1970-01-01
  • 2013-06-12
  • 1970-01-01
  • 2019-08-23
  • 1970-01-01
  • 2012-01-10
  • 2013-03-22
  • 2018-12-16
相关资源
最近更新 更多