Reputation: 27
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
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
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:
Upvotes: 3
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
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
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
Click Console tab of developer tool
In the above picture there is only one frame.
FIND ELEMENT
$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
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.
Right click on the element (here the link)
Click on inspect
. It opens the inspect console (Elements tab) in your browser, and highlights the clicked element.
Right click on the element in the elements tab
Choose copy
Copy Xpath
In your code, replace By.linkText
with By.xpath
Paste the value as an string into the xpath("you paste here")
Or simpler, watch this video
Upvotes: 1