使用 Java 和 Selenium WebDriver 在表单和 iframe 中查找元素 [英] Find elements inside forms and iframe using Java and Selenium WebDriver

查看:34
本文介绍了使用 Java 和 Selenium WebDriver 在表单和 iframe 中查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问

下存在的元素<iFrame><表格>元素</iFrame>
.

你能帮我访问这些我正在使用 Selenium Webdriver 和 JAVA 的'元素'吗?

遇到的问题:能够到达目标页面(存在上述元素的地方),但我的代码无法识别这些元素.

XML 结构概述:

<form action="https://abcd/efgh/" name="outerForm" method="post" target="iFrameTitle"><iframe width="700" height="600" src="" title="Java 测试框架" name="iFrameTitle" scrolling="auto" frameborder="0"><form id="innerFormID" name="innerForm" action="/xxx/xxxx/yyyy/zzzz/" method="post" autocomplete="off"><fieldset id="ncDetailsInner"><div id="element1"><label for="label1"><abbr title="必填字段">*</abbr></label><input name="label2" type="text" maxlength="30" id="cardHolder" value="" >

<div id="element2"><label for="label3">Label3 <abbr title="必填字段">*</abbr></label><div id="element3"><label for="label4">Label4<abbr title="必填字段">*</abbr></label><input id="label5" name="labelname5" type="text" maxlength="19" value="">

<div id="element4"><label for="label6">Label6</label><input id="label7" name="labelname7" type="text" size="2" maxlength="2" value="" class="text disabled" disabled="">

</fieldset></表单></iframe></表单>

代码提取:

WebDriverWait wait_iframe = new WebDriverWait(driver, 20000);wait_iframe.until(ExpectedConditions.visibilityOfElementLocated((By.id("element2"))));call_function(sh1.getCell(col + 10, row).getContents(),sh1.getCell(col + 11, row).getContents(),sh1.getCell(col + 12, row).getContents(),sh1.getCell(col + 14, row).getContents());public static void called_funciton(String string1, String string2,字符串 string3, 字符串 string4) {driver.findElement(By.name("Element1 Name")).sendKeys(string1);driver.findElement(By.id("Element2 ID")).sendKeys(string2);driver.findElement(By.id("Element3 ID")).sendKeys(string3);driver.findElement(By.id("Element4 ID")).sendKeys(string4);driver.findElement(By.name("submitButton")).click();}

如果需要更多详细信息,请告诉我!

解决方案

在尝试搜索 iframe 中的元素之前,您必须将 Selenium 焦点切换到 iframe.

在搜索 iframe 中的元素之前试试这个:

driver.switchTo().frame(driver.findElement(By.name("iFrameTitle")));

I'm trying to access elements that are present under <form> <iFrame> <form> elements </form> </iFrame> </form>.

Could you help me on accessing these 'elements', which I'm working with Selenium Webdriver and JAVA?

Issue Encountered: Able to reach the destination page (where the above elements are present), but those elements are not recognized with my code.

Overview of XML structure:

<body>
    <form action="https://abcd/efgh/" name="outerForm" method="post" target="iFrameTitle">
        <iframe width="700" height="600" src="" title="Frame for Java Test" name="iFrameTitle" scrolling="auto" frameborder="0">
            <form id="innerFormID" name="innerForm" action="/xxx/xxxx/yyyy/zzzz/" method="post" autocomplete="off">
                <fieldset id="ncDetailsInner">
                    <div id="element1">
                        <label for="label1">
                        <abbr title="Required field">*</abbr></label>
                        <input name="label2" type="text" maxlength="30" id="cardHolder" value="" >
                    </div>

                    <div id="element2">
                        <label for="label3">Label3 <abbr title="Required field">*</abbr></label>
                        <div id="element3">
                            <label for="label4">Label4<abbr title="Required field">*</abbr></label>
                            <input id="label5" name="labelname5" type="text" maxlength="19" value="">
                        </div>

                        <div id="element4">
                            <label for="label6">Label6</label>
                            <input id="label7" name="labelname7" type="text" size="2" maxlength="2" value="" class="text disabled" disabled="">
                        </div>
                    </div>
                </fieldset>
            </form> 

        </iframe>
    </form>
</body>

Code Extract:

WebDriverWait wait_iframe = new WebDriverWait(driver, 20000);

wait_iframe.until(ExpectedConditions.visibilityOfElementLocated((By.id("element2"))));

calling_function(sh1.getCell(col + 10, row).getContents(), 
                sh1.getCell(col + 11, row).getContents(),
                sh1.getCell(col + 12, row).getContents(), 
                sh1.getCell(col + 14, row).getContents());                      

public static void called_funciton(String string1, String string2,
        String string3, String string4) {
        driver.findElement(By.name("Element1 Name")).sendKeys(string1);
        driver.findElement(By.id("Element2 ID")).sendKeys(string2);
        driver.findElement(By.id("Element3 ID")).sendKeys(string3);
        driver.findElement(By.id("Element4 ID")).sendKeys(string4);
        driver.findElement(By.name("submitButton")).click();
    }

Do let me know if require any further details!

解决方案

Before you try searching for the elements within the iframe you will have to switch Selenium focus to the iframe.

Try this before searching for the elements within the iframe:

driver.switchTo().frame(driver.findElement(By.name("iFrameTitle")));

这篇关于使用 Java 和 Selenium WebDriver 在表单和 iframe 中查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆