Yan
Yan

Reputation: 77

How to find an element on the website with frames using Selenium?

I'm trying to locate an element (containing "791") on a website containing frames (the element, however is located in the Top Window). For some reason, Selenium cannot find the element. What's the right way to locate it? Thanks!

public class StarOnline {

    static WebDriver driver;
    static String baseUrl;

    public static void main(String[] args) throws InterruptedException {

        baseUrl = "https://www.staronline.org.uk/demo_register.asp";

                System.setProperty("webdriver.chrome.driver", "C:\\Automation\\libs\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();

                driver.get(baseUrl);

        List<WebElement> checkbox = driver.findElements(By.className(".frm"));
        System.out.println("list size = " + checkbox.size());
        for (WebElement ell:checkbox)
        {
            if (ell.getText().contains("791"))
                ell.click();
            else System.out.println("Element not found");
            break;
        }

Upvotes: 0

Views: 762

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193388

The element you are trying to locate is an option element within the dropdown with id as mainstar_. The dropdown is on the Top Window so you don't need to switch to any frame as such to locate the element. So we will select the option with text as 791 through selectByValue() method of Select class. If you observe closely, the option with text 791 contains the visible text Alcohol Star. Hence when we will select the option Alcohol Star will be chosen from the dropdown. Here is the minimal code:

package demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class Q45770184_frame 
{
    static WebDriver driver;
    static String baseUrl;

    public static void main(String[] args) 
    {

    baseUrl = "https://www.staronline.org.uk/demo_register.asp";
    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get(baseUrl);
    WebElement drop_down = driver.findElement(By.id("mainstar_"));
    Select select = new Select(drop_down);
    select.selectByValue("791");
    }   
}

Upvotes: 0

santhosh kumar
santhosh kumar

Reputation: 2019

This is not the direct answer of this question but a mere approach. We need to use switchToFrame method.

If you need to switch to sibling of the frame that you are in now, we should probably come out of all frames and then switch to that sibling frame from the root. For example consider the below image,

Selenium Handling Frames

Switch to Frame 1 from root:

driver.switchTo().frames("Frame 1");

Switch to frame 2 which is outside of Frame 1:

//Come to root from Frame 1
driver.switchTo().defaultContent();

//Now switch to Frame 2
driver.switchTo().frames("Frame 2");

This is applicable for nested Frames/iFrames also. Hope this helps.

Thanks.

Upvotes: 1

Related Questions