简体   繁体   中英

Not able to identify the iframes using selenium

Trying to find and switch to iframe in the page, but it is showing the number of iframe as 0.

The iframe is present in html code.

Html code:

<iframe id="walkme-native-functions" style="display: none; position: absolute;"></iframe>
<iframe id="walkme-proxy-iframe" src="about:blank" style="display: none; visibility: hidden;"></iframe>

Java code:

 List<WebElement> frames=chromeSelDriver.findElements(By.tagName("iframe"));
 System.out.println(frames.size());
 System.out.println(frames);
 chromeSelDriver.switchTo().frame(0);

Html code:

<iframe id="walkme-native-functions" style="display: none; position: absolute;"></iframe>
<iframe id="walkme-proxy-iframe" src="about:blank" style="display: none; visibility: hidden;"></iframe>

Output of Java code:

0
[]

Jar / DOCS available at: http://developer.torello.directory/JavaHTML/Version%201/1.4/index.html

import Torello.HTML.*;
import Torello.HTML.NodeSearch.*;

import java.util.*;
import java.io.*;

public class IFramer
{
    public static final String html =
        "<iframe id=\"walkme-native-functions\" style=\"display: none; position: absolute;\"></iframe>" + 
        "<iframe id=\"walkme-proxy-iframe\" src=\"about:blank\" style=\"display: none; visibility: hidden;\"></iframe>";

    public static void main(String[] argv) throws IOException
    {
        Vector<HTMLNode> page = HTMLPage.getPageTokens(html, false);
        int[] posArr = TagNodeFind.all(page, TC.Both, "iframe");
        for (int pos : posArr)
        {
            TagNode tn = (TagNode) page.elementAt(pos);
            System.out.println(tn.str);
            if (! tn.isClosing)
                System.out.println("TagNode ID Attribute: [" + tn.AV("id") + "]");
        }
    }
}

Produces this output:

@cloudshell:~$ export CLASSPATH=.:~/JavaHTML-1.4.jar
@cloudshell:~$ java IFramer
<iframe id="walkme-native-functions" style="display: none; position: absolute;">
TagNode ID Attribute: [walkme-native-functions]
</iframe>
<iframe id="walkme-proxy-iframe" src="about:blank" style="display: none; visibility: hidden;">
TagNode ID Attribute: [walkme-proxy-iframe]
</iframe>

Selenium has been written such that it does NOT allow interaction with hidden elements. The idea behind it is that if a person cannot perform an action, then neither can Selenium. Since both your iframes are hidden, selenium is unable to recognize these elements.

However, you can use Javascript with Selenium to interact with a hidden element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM