2

I am trying to run the below code but getting error. Can you please point out the issue

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Menu {

    public static void main(String[] args) throws InterruptedException {
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("acceptInsecureCerts", true);
        capabilities.setJavascriptEnabled(true);
        capabilities.setCapability("marionette", true);
        System.setProperty("webdriver.gecko.driver","C:/Users/ikram/Downloads/geckodriver-v0.18.0-win64/geckodriver.exe");
        WebDriver driver =new FirefoxDriver();

        driver.get("http://awqaf.texpo.com/");
        driver.switchTo().defaultContent();
        //driver.switchTo().frame(0); // use this if it's in frame and you don't know how to locate it
        //WebElement aboutdrop = (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[contains(text(),'ABOUT US')]")));
        WebElement aboutdrop=driver.findElement(By.xpath("//a[contains(text(),'ABOUT US')]"));
        List<WebElement> about=aboutdrop.findElements(By.tagName("option"));

        for (int i=0;i<=about.size();i++){
            String value = about.get(i).getText();
            System.out.println(value);

        }
    }
}

Error description

1500455460357   geckodriver INFO    geckodriver 0.18.0
1500455460362   geckodriver INFO    Listening on 127.0.0.1:11115
1500455461028   geckodriver::marionette INFO    Starting browser C:\Program Files\Mozilla Firefox\firefox.exe with args ["-marionette"]
1500455466131   Marionette  INFO    Listening on port 53870
Jul 19, 2017 2:11:06 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //a[contains(text(),'ABOUT US')]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'SAIF', ip: '10.20.1.95', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\ikram\AppData\Local\Temp\rust_mozprofile.daCtPib7HZe9, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=54.0.1, platformVersion=6.1, moz:processID=7136.0, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}]
Session ID: ef7987ee-c265-4f09-a38a-67c357c8d97e
*** Element info: {Using=xpath, value=//a[contains(text(),'ABOUT US')]}
    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:150)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:509)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
    at Menu.main(Menu.java:25)
1
  • 1
    Your element which you are trying to access using WebElement aboutdrop=driver.findElement(By.xpath("//a[contains(text(),'ABOUT US')]")); is not correct. Please provide relevant HTML so that a proper solution can be given. Jul 19, 2017 at 9:31

2 Answers 2

2

Use linkText element Locator method instead of XPath.

    driver.get("http://awqaf.texpo.com/"); 
    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element =wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("ABOUT US"))); 
   // WebElement aboutdrop=driver.findElement(By.linkText("ABOUT US"));

Also, try with removing driver.switchTo().defaultContent(); this line.

Generally switchTo().defaultContent(); method is used to go back on previous working window. So if it requires in your case then & then you may use.

1

You are getting NoSuchElementException which generally happens in 2 cases.

Case 1: The WebElement that you are trying to find is not present (or incorrect) on the page. So check if this is correct By.xpath("//a[contains(text(),'ABOUT US')]")

Case 2: You are trying to find the WebElement even before the page has completed loaded. Which I think is your case. In this scenario you should wait for the WebElement to get completely loaded then find it.

Below is the sample code to wait for the WebElement to load.

//This will initialize a object of WebDriverWait Class with the wait time 
//of 30 seconds before throwing the NoSuchElementException.
WebDriverWait wait = new WebDriverWait( driver , 30);

wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//a[contains(text(),'ABOUT US')]")));

Let me know if this works for you or not. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.