2

I have this piece of code

<a href="/iot/apply/device.do" id="subMenu1" class="fortification55"
                                                        onclick="$('#deviceinfo').hide()">Apply</a>

I am trying to link by href using

getDriver().findElement(By.name("subMenu1")).click();

But i got this error

org.openqa.selenium.NoSuchElementException: Unable to find element with name == subMenu1 (WARNING: The server did not provide any stacktrace information)
4
  • 3
    I think you should use By.id("subMenu1") Commented Aug 22, 2016 at 11:57
  • 2
    You can also directly use getDriver().findElement(By.linkText("Apply")).click(); Commented Aug 22, 2016 at 11:58
  • you can also use xpaths: //a[@id='subMenu1'] or //a[text()='Apply'] Commented Aug 22, 2016 at 12:07
  • 1
    You can use as : getDriver().findElement(By.className("fortification55")).click(); Commented Aug 22, 2016 at 12:13

2 Answers 2

3
+500

As the element is having the onclick Event, the WebElement is a JavaScript enabled element. So to invoke click() on the element you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using cssSelector and only href attribute:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='/iot/apply/device.do']"))).click();
    
  • Using xpath and only href attribute:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='/iot/apply/device.do' and text()='Apply']"))).click();
    
  • Using a canonical cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.fortification55#subMenu1[href='/iot/apply/device.do']"))).click();
    
  • Using a canonical xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='fortification55' and @id='subMenu1'][@href='/iot/apply/device.do' and text()='Apply']"))).click();
    
  • Note : You have to add the following imports :

    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.By;
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

Sign up to request clarification or add additional context in comments.

1 Comment

@nice answer that should resolve the issue , and nice explination well done
0

the following code should work :

By.xpath("//a[@href='/iot/apply/device.do']")

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.