如何使用Selenium WebDriver导航框架内的一个子框架?[英] How to navigate a subframe inside a frameset using Selenium WebDriver?

本文是小编为大家收集整理的关于如何使用Selenium WebDriver导航框架内的一个子框架?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我需要从表" TD"表获得结果.但是在我能做之前,我需要浏览包含它的框架.框架是属于大型机的框架元素之一.我尝试使用所有类型的导航"儿童"副框架非有效的工作:

 driver.switchTo().defaultContent();
    Thread.sleep(1000);
     driver.switchTo().frame("mainFrame.0.fr_resultsNav~ResultsMaxGroupTemplate0.9766101221774707");
    driver.switchTo().frame("main.Frame.1.fr_resultsNav~ResultsMaxGroupTemplate0.8811790466176727");

// even: driver.switchTo().frame("mainFrame.0.fs_main");

以下是网页的简短布局:

<frame src="banner.asp" name="topFrame" scrolling="no" noresize="noresize" id="topFrame" title="topFrame">

<frame src="" name="mainFrame" id="mainFrame" title="mainFrame" wd_frame_id_="5f4c10bc7e0960070bfda831655b8b0c">

    <frameset id="fs_main" border="0" frameborder="no" framespacing="0" rows="70,87,*">
.....................

        <frameset id="fs_content" cols="23%,*" framespacing="0" frameborder="no">
.....................
            <frameset cols="*,9" id="LeftFrameSet" framespacing="0" frameborder="no">
.....................
            <frame frameborder="0" name="fr_classification~ResultsMaxGroupTemplate0.609021034867735" title="Results Classification Frame" id="fr_classification~ResultsMaxGroupTemplate0.609021034867735" src="/lnacui2api/results/shared/waitMessage.do?wmibIdRand=61_T16938265013_rand_1363544453847" scrolling="Auto" onload="paintResultsBorder('ResultsMaxGroupTemplate0.609021034867735');">

....................

<form name="results_listview_ResultsListForm" method="post" action="/lnacui2api/results/listview/listview.do" id="results_listview_ResultsListForm">

..........

<td nowrap="" height="20"> <span id="new">&nbsp;All Results</span> (294)</td>
</form>

....................

我需要在框架导航之前浏览框架?我阅读了文档.所有Internet示例都给出一个简单的示例代码:driver.switchto().帧(" Mainframe.0.Child").在这种情况下它不起作用.请看一下上面的脚本.

推荐答案

我同意,您不能直接切换到子框架.另外,请确保每次要切换框架时都切换到defaultContent(driver.switchTo.defaultContent).关于您的示例,driver.switchTo().frame("mainFrame.0.child") ---这也可能起作用,但是您需要摆脱不必要的引号.

其他推荐答案

从零开始找到主帧的索引,然后使用

driver.switchTo.frame(mainFrameindex);

然后在主帧中找到子框架的索引

driver.switchTo.frame(subFrameIndex);

如果不先切换到父框架,您就不能直接切换到子框架.这就是它的工作方式.

其他推荐答案

通过将链接方法一起使用,一旦您switchTo().defaultContent,您可以通过tagName创建可用帧的临时列表,然后转到该特定帧的索引...

例如

driver.switchTo()defaultContent();
driver.switchTo().frame(driver.findElement(By.tagName("frameset")).findElements(By.tagName("frame")).get(2));

本文地址:https://www.itbaoku.cn/post/2091124.html

问题描述

I need to get the result from the table "td". But before I can do that I need to navigate a frame that contains it. The frame is one of the frameset elements that belongs to the mainFrame. I tried to use all types of navigating the "child" subframe non of which works:

 driver.switchTo().defaultContent();
    Thread.sleep(1000);
     driver.switchTo().frame("mainFrame.0.fr_resultsNav~ResultsMaxGroupTemplate0.9766101221774707");
    driver.switchTo().frame("main.Frame.1.fr_resultsNav~ResultsMaxGroupTemplate0.8811790466176727");

// even: driver.switchTo().frame("mainFrame.0.fs_main");

The following is the brief layout of the webpage:

<frame src="banner.asp" name="topFrame" scrolling="no" noresize="noresize" id="topFrame" title="topFrame">

<frame src="" name="mainFrame" id="mainFrame" title="mainFrame" wd_frame_id_="5f4c10bc7e0960070bfda831655b8b0c">

    <frameset id="fs_main" border="0" frameborder="no" framespacing="0" rows="70,87,*">
.....................

        <frameset id="fs_content" cols="23%,*" framespacing="0" frameborder="no">
.....................
            <frameset cols="*,9" id="LeftFrameSet" framespacing="0" frameborder="no">
.....................
            <frame frameborder="0" name="fr_classification~ResultsMaxGroupTemplate0.609021034867735" title="Results Classification Frame" id="fr_classification~ResultsMaxGroupTemplate0.609021034867735" src="/lnacui2api/results/shared/waitMessage.do?wmibIdRand=61_T16938265013_rand_1363544453847" scrolling="Auto" onload="paintResultsBorder('ResultsMaxGroupTemplate0.609021034867735');">

....................

<form name="results_listview_ResultsListForm" method="post" action="/lnacui2api/results/listview/listview.do" id="results_listview_ResultsListForm">

..........

<td nowrap="" height="20"> <span id="new">&nbsp;All Results</span> (294)</td>
</form>

....................

Do I need to navigate the frameset before I can navigate a subframe? I read the documentation. All internet examples give a simple sample code: driver.switchTo().frame("mainFrame.0.child"). It does not work in this case. Please, take a look at the above script.

推荐答案

I agree, you cannot directly switch to a child frame. Also, make sure to switch to the defaultContent (driver.switchTo.defaultContent) every time you want to switch frame. With regard to your example, driver.switchTo().frame("mainFrame.0.child") --- this could also work, but you need to get rid of unnecessary quotation marks.

其他推荐答案

Find the index of main frame starting from zero then use

driver.switchTo.frame(mainFrameindex);

Then find the index of sub frame in the main frame

driver.switchTo.frame(subFrameIndex);

You cannot directly switch to a child frame without first switching to the parent frame. This is how it works.

其他推荐答案

Through chaining methods together, once you switchTo().defaultContent, you can create temporary lists of available frames through findElements() by tagName and go to that specific frames' index...

For example

driver.switchTo()defaultContent();
driver.switchTo().frame(driver.findElement(By.tagName("frameset")).findElements(By.tagName("frame")).get(2));