Prakash Mohanraj
Prakash Mohanraj

Reputation: 27

How to find element in frame in Selenium

I used below code for finding the element in the frame but I got the error kindly explain by this framebyinedex, framebystring, framebywebelement:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver;

public class Framebyindex 
{
    public static void main(String[] args) 
    {
        WebDriver f1=new FirefoxDriver();
        f1.get("http://spicareers.com/spicareers/");
        f1.switchTo().frame(1);
        f1.findElement(By.linkText(" .Net - Senior Developer ")).click();
    }
}

The error is:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":" .Net - Senior Developer "}

Upvotes: 2

Views: 17228

Answers (6)

nick318
nick318

Reputation: 575

By using: https://github.com/nick318/FindElementInFrames

you can write following:

    SearchByFramesFactory searchFactory = new SearchByFramesFactory(driver);
    
    SearchByFrames searchInFrame = searchFactory.search(() -> driver.findElement(By.xpath("//*[contains(text(), ' .Net - Senior Developer ')]")));
    searchInFrame.getElem().ifPresent(WebElement::click);

   

Upvotes: 0

Davide Patti
Davide Patti

Reputation: 3471

Before you have to switch to the frame:

        driver.get("http://spicareers.com/spicareers/");
        WebElement frame= driver.findElement(By.xpath("//frame[@name='JRAMPSMainFrame']"));
        driver.switchTo().frame(frame);

and after you could try with:

        WebElement myEl= driver.findElement(By.xpath("//*[contains(text(), ' .Net - Senior Developer ')]"));
        myEl.click();

Finally, if you want to switch back:

driver.switchTo().defaultContent();

EDIT

The interested element is inside the iframe with name "JRAMPSMainFrame". If you inspect your html, you could notice this:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Upvotes: 3

iamsankalp89
iamsankalp89

Reputation: 4749

You can use this code, First enter into the frame using name locator and try to locate the career option then

Try this code, I have modified your code:

public class Framebyindex 
{
    public static void main(String[] args) 
    {
        WebDriver f1=new FirefoxDriver();
        f1.get("http://spicareers.com/spicareers/");
        f1.switchTo().frame( f1.findElement(By.name("JRAMPSMainFrame")));
WebElement netCareer= f1.findElement(By.xpath("//*[contains(text(), ' .Net - Senior Developer ')]"));
        netCareer.click();
    }
}

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193348

If you take a closer look at the HTML of http://spicareers.com/spicareers/ the WebElement with text as .Net - Senior Developer is within an <iframe> tag. So before accessing the intended WebElement we have to switch to the frame first either through the frame_name, frame_id or frame_index as follows:

System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://spicareers.com/spicareers/");
driver.switchTo().frame("JRAMPSMainFrame"); //using "frame_name"
WebElement elem = driver.findElement(By.xpath("//*[contains(text(), ' .Net - Senior Developer ')]"));
elem.click();

Upvotes: 0

StrikerVillain
StrikerVillain

Reputation: 3786

Seems like you have selected a wrong frame or the link text might contain more spaces.

Chrome developer tools is your best friend in this situation. Use it to find the element correctly and to identify the frame.

SELECT FRAME

  1. Open the application in chrome.
  2. Open developer tools
  3. Click Console tab of developer tool

  4. Select you frame from the drop down frame selector

In the above picture there is only one frame.

FIND ELEMENT

  1. After selecting the frame, type $x("//a[normalize-space(.) = '.Net - Senior Developer']") to check your xpath.

$x("") is the function for checking xpath in browser.

Using the above mechanism find the frame and the element.

Cheers!

Upvotes: 1

Sal-laS
Sal-laS

Reputation: 11659

NoSuchElementException simply means that, the selenium did not detect a link with the text as you asked it.

I suggest you to work with use by.xpath to resolve it. To get the xpath of the element, take the below steps on your chrome, or firefox browser.

  1. Right click on the element (here the link)

  2. Click on inspect. It opens the inspect console (Elements tab) in your browser, and highlights the clicked element.

  3. Right click on the element in the elements tab

  4. Choose copy

  5. Copy Xpath

  6. In your code, replace By.linkText with By.xpath

  7. Paste the value as an string into the xpath("you paste here")

Or simpler, watch this video

Upvotes: 1

Related Questions