使用 Java 和 Selenium WebDriver 在表单和 iframe 中查找元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24247490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find elements inside forms and iframe using Java and Selenium WebDriver
提问by PraveenKS
I'm trying to access elements that are present under <form> <iFrame> <form> elements </form> </iFrame> </form>
.
我正在尝试访问存在于<form> <iFrame> <form> elements </form> </iFrame> </form>
.
Could you help me on accessing these 'elements', which I'm working with Selenium Webdriver and JAVA?
你能帮我访问这些我正在使用 Selenium Webdriver 和 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:
XML 结构概述:
<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!
如果需要任何进一步的细节,请告诉我!
采纳答案by SDET
Before you try searching for the elements within the iframe you will have to switch Selenium focus to the iframe.
在尝试搜索 iframe 中的元素之前,您必须将 Selenium 焦点切换到 iframe。
Try this before searching for the elements within the iframe:
在搜索 iframe 中的元素之前试试这个:
driver.switchTo().frame(driver.findElement(By.name("iFrameTitle")));
回答by YouNIC Technolabs
When using an iframe, you will first have to switch to the iframe, before selecting the elements of that iframe
使用 iframe 时,您首先必须切换到 iframe,然后才能选择该 iframe 的元素
You can do it using:
您可以使用:
driver.switchTo().frame(driver.findElement(By.id("frameId")));
//do your stuff
driver.switchTo().defaultContent();
In case if your frameId is dynamic, and you only have one iframe, you can use something like:
如果您的 frameId 是动态的,并且您只有一个 iframe,则可以使用以下内容:
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
回答by Quentin Martinez
On Selenium >= 3.41 (C#)the rigth syntax is:
在Selenium >= 3.41 (C#) 上,正确的语法是:
webDriver = webDriver.SwitchTo().Frame(webDriver.FindElement(By.Name("icontent")));