Selenium Javascript - 以编程方式访问 iframe 中的 Web 元素
Selenium Javascript - Access webelements within iframe programatically
我正在尝试以编程方式访问 iFrame 中的 Web 元素。使用下面的方法检查相同的
- 将元素的 URL 与基本 URI
进行比较
- 如果相同,则 Web 元素在主 DOM 中,否则在 iFrame 中
- 使用 switchTo.frame() 切换到 iFrame 并执行操作
在上述方法中,以下方法用于获取元素的URI
document.getElementsByTagName("a")[0].baseURI;
或
document.getElementsByClassName("w3-btn w3-border")[0].baseURI
查询:
有没有使用xpath获取baseURI的函数或方法?我不想使用标记名或类名。
另外,当多个 iFrame 或另一个 iFrame 中的 iFrame 时如何实现相同。
如有其他方法请多多指教
以下代码将有助于自动切换 iframe....
此代码处理 2 级 iframe,即
iframe -1 --------> 级别 1
iframe - 2 --------> 级别 1
---- child iframe - 1 [under iframe 2] ------> Level 2
iframe - 3 --------> 级别 1
代码
async function switchtoiframe(wdriver, xpath) {
//console.log(" ---- Code to switch iframes start ---- ");
wdriver.switchTo().defaultContent();
let parentEleFound = false;
let childEleFound = false;
let parentframeId;
let childframeId;
// find number iframes in main content
await wdriver.findElements(By.tagName('iframe')).then(async function (pIframe) {
console.log("Total iframe count - " + pIframe.length);
// if number of iframes found then switch to iframe
if (pIframe.length > 0) {
for (let i = 0; i<pIframe.length; i++)
{
await wdriver.switchTo().frame(i); // switch to parent iframe
// find number of child iframes in parent frame
await wdriver.findElements(By.tagName('iframe')).then(async function (cIframe) {
//console.log("total child iframe count - " + cIframe.length + " in parent frame " + i);
// if number of iframes found then switch to child iframe
if (cIframe.length > 0) {
for (let j = 0; j<cIframe.length; j++)
{
await wdriver.switchTo().frame(j); // switch to child iframe
// check whether element is available in child iframe
await wdriver.findElements(By.xpath(xpath)).then(async function (cElement)
{
console.log("child for loop : j - "+ j);
console.log("child element - "+ cElement.length);
if(cElement.length == 1)
{
childframeId = j;
childEleFound = true;
}
// switch back to parent iframe
wdriver.switchTo().parentFrame(i);
});
console.log("Element found in child iframe - "+ childEleFound)
if (childEleFound == true ){
// switch to child iframe where element is found
await wdriver.switchTo().frame(childframeId)
break; // break for loop [j]
}
}
}
});
if (childEleFound == true ){ // if element found in child iframe then break the parent for loop [i] also.
break;
}
// if no child iframes then find element in parent iframe
await wdriver.findElements(By.xpath(xpath)).then(async function (pElement)
{
console.log("parent for loop : i - "+ i);
console.log("parent element - "+ pElement.length);
if(pElement.length == 1)
{
parentframeId = i;
parentEleFound = true;
}
// switch back to main content
wdriver.switchTo().defaultContent();
});
console.log("Element found in parent iframe - "+ parentEleFound)
if (parentEleFound == true ){
// switch to iframe where element is found
await wdriver.switchTo().frame(parentframeId)
break;
}
}
}
});
console.log(" ---- Code to switch iframes end ---- ");
}
我正在尝试以编程方式访问 iFrame 中的 Web 元素。使用下面的方法检查相同的
- 将元素的 URL 与基本 URI 进行比较
- 如果相同,则 Web 元素在主 DOM 中,否则在 iFrame 中
- 使用 switchTo.frame() 切换到 iFrame 并执行操作
在上述方法中,以下方法用于获取元素的URI
document.getElementsByTagName("a")[0].baseURI;
或
document.getElementsByClassName("w3-btn w3-border")[0].baseURI
查询:
有没有使用xpath获取baseURI的函数或方法?我不想使用标记名或类名。
另外,当多个 iFrame 或另一个 iFrame 中的 iFrame 时如何实现相同。
如有其他方法请多多指教
以下代码将有助于自动切换 iframe.... 此代码处理 2 级 iframe,即
iframe -1 --------> 级别 1
iframe - 2 --------> 级别 1
---- child iframe - 1 [under iframe 2] ------> Level 2
iframe - 3 --------> 级别 1
代码
async function switchtoiframe(wdriver, xpath) {
//console.log(" ---- Code to switch iframes start ---- ");
wdriver.switchTo().defaultContent();
let parentEleFound = false;
let childEleFound = false;
let parentframeId;
let childframeId;
// find number iframes in main content
await wdriver.findElements(By.tagName('iframe')).then(async function (pIframe) {
console.log("Total iframe count - " + pIframe.length);
// if number of iframes found then switch to iframe
if (pIframe.length > 0) {
for (let i = 0; i<pIframe.length; i++)
{
await wdriver.switchTo().frame(i); // switch to parent iframe
// find number of child iframes in parent frame
await wdriver.findElements(By.tagName('iframe')).then(async function (cIframe) {
//console.log("total child iframe count - " + cIframe.length + " in parent frame " + i);
// if number of iframes found then switch to child iframe
if (cIframe.length > 0) {
for (let j = 0; j<cIframe.length; j++)
{
await wdriver.switchTo().frame(j); // switch to child iframe
// check whether element is available in child iframe
await wdriver.findElements(By.xpath(xpath)).then(async function (cElement)
{
console.log("child for loop : j - "+ j);
console.log("child element - "+ cElement.length);
if(cElement.length == 1)
{
childframeId = j;
childEleFound = true;
}
// switch back to parent iframe
wdriver.switchTo().parentFrame(i);
});
console.log("Element found in child iframe - "+ childEleFound)
if (childEleFound == true ){
// switch to child iframe where element is found
await wdriver.switchTo().frame(childframeId)
break; // break for loop [j]
}
}
}
});
if (childEleFound == true ){ // if element found in child iframe then break the parent for loop [i] also.
break;
}
// if no child iframes then find element in parent iframe
await wdriver.findElements(By.xpath(xpath)).then(async function (pElement)
{
console.log("parent for loop : i - "+ i);
console.log("parent element - "+ pElement.length);
if(pElement.length == 1)
{
parentframeId = i;
parentEleFound = true;
}
// switch back to main content
wdriver.switchTo().defaultContent();
});
console.log("Element found in parent iframe - "+ parentEleFound)
if (parentEleFound == true ){
// switch to iframe where element is found
await wdriver.switchTo().frame(parentframeId)
break;
}
}
}
});
console.log(" ---- Code to switch iframes end ---- ");
}