selenium test throws org.openqa.selenium.NoSuchElementException

I have written a selenium test for yahoo website. In this test I am testing Yahoo News. But this code is throwing an Exception. So please solve the problem? I am using Eclipse IDE, and Firefox as browser.

Code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Tests {

    WebDriver driver;
    Wait<WebDriver> wait;
    boolean result;

    Tests() {
        driver = new FirefoxDriver();
        wait = new WebDriverWait(driver, 30);
        driver.get("http://www.yahoo.com/");
    }

    public static void main(String arg[]) {
        new Tests().news();
    }

    public boolean news() {
        try {
            System.out.print("Testing News... ");
            driver.findElement(By.linkText("https://www.yahoo.com/news/")).click();
            driver.findElement(By.partialLinkText("/news/world/")).click();
            wait.until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver webDriver) {
                    return webDriver.findElement(By.id("th-title")) != null;
                }
            });
            return driver.findElement(By.id("th-title")).getText().contains("World");
        }
        catch(Exception exp) {
            exp.printStackTrace();
            return false;
        }
    }
}

Exception:

Testing News... org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"https://www.yahoo.com/news/"}
Command duration or timeout: 21.55 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'Jahanzeb', ip: '10.99.14.207', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79'
*** Element info: {Using=link text, value=https://www.yahoo.com/news/}
Session ID: 7576b452-dcb2-448f-844c-6c8b499561f1
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=46.0.1}]
FAILED
    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.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByLinkText.findElement(By.java:246)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
    at Tests.news(Tests.java:72)
    at Main.main(Main.java:14)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"https://www.yahoo.com/news/"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'Jahanzeb', ip: '10.99.14.207', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79'
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/[email protected]/components/driver-component.js:10770)
    at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/[email protected]/components/driver-component.js:10779)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/[email protected]/components/command-processor.js:12661)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/[email protected]/components/command-processor.js:12666)
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/[email protected]/components/command-processor.js:12608)

Answer

Please examine Yahoo page.
Please press Ctrl-F and try to find a text: “https://www.yahoo.com/news/“>
There is no such a text there.

But if you look into a html source of this page,
you will find this A tag with href attriibutte = “”https://www.yahoo.com/news/“:

<a class="C(#fff) Td(n) Td(u):h" href="https://www.yahoo.com/news/" 
 data-reactid=".pgwo63s1xy.$tgtm-UH-0-Header.1.0.0:$news.0">News</a>

But a “link text” of this link is not “https://www.yahoo.com/news/“, but “News” .
The By#linkText method is looking for the link text (in this case “News”), not for href attributte.

You need to replace this command:

 driver.findElement(By.linkText("https://www.yahoo.com/news/")).click(); 

with this one:

driver.findElement(By.linkText("News")).click();

Leave a Reply

Your email address will not be published. Required fields are marked *