import java.awt.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LinksSpecificLoc {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver wd;
wd = null;
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "null");
System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver\\geckodriver-v0.24.0-win64 (1)\\geckodriver.exe");
wd = new FirefoxDriver();
wd.get("https://www.indiatoday.in/");
wd.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
WebElement Box = wd.findElement(By.xpath("//*[@class ='widget-wrapper top_stories_ordering']"));
java.util.List<WebElement> links = Box.findElements(By.tagName("a"));
int linksno = links.size();
System.out.println(linksno);
//WebElement tenthlink = links.get(10);
//System.out.println(tenthlink.getText());
for(int i =0;i<linksno;i++)
{
String linkval =links.get(i).getText();
System.out.println(linkval);
links.get(i).click();
String title =wd.getTitle();
System.out.println(title);
wd.get("https://www.indiatoday.in/");
WebElement Box1 = wd.findElement(By.xpath("//*[@class ='widget-wrapper top_stories_ordering']"));
java.util.List<WebElement> links1 = Box1.findElements(By.tagName("a"));
}
}
}
Instructor
Ashish Thakur Replied on 02/08/2019
Please share the complete exception stack trace.
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Web element reference not seen before: 6239eccd-fe72-492b-9ec8-e19fe9a8d2e2
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'WTIN05200385L', ip: '10.252.171.185', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_211'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 68.0, javascriptEnabled: true, moz:accessibilityChecks: false, moz:buildID: 20190705220548, moz:geckodriverVersion: 0.24.0, moz:headless: false, moz:processID: 9684, moz:profile: C:\Users\test\AppData..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: a8a15e34-ae16-4ccc-9d14-31bfbd69bac9
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:166)
at LinksSpecificLoc.main(LinksSpecificLoc.java:30)
Instructor
Ashish Thakur Replied on 05/08/2019
Please go to LinksSpecificLoc.java line number 30 for the possible error.
The code is not able to find the element with the specified locator.
Please note that if the locator you're trying to use is correct, then try updating the browser and driver and then try executing the same code.