Home > Mobile >  I am having problems identifying the element using selenium in java
I am having problems identifying the element using selenium in java

Time:10-30

I am having problems trying to identify the element using selenium and java...for the following link:

<a id="newSearchNavigateHeadingEventId_0" class="search-heading" ng-href="book/event?eid=757231&amp;" target="_self" href="book/event?eid=757231&amp;">
<h2 ng-bind="event.EventDisplayName" class="ng-binding">Makerspace Docklands - Safety Induction</h2>
</a>

I have tried the following ...

WebElement we = myDriver. findElement(By.linkText("Makerspace Docklands - Safety Induction"));
WebElement we =  myDriver.findElement(By.id("newSearchNavigateHeadingEventId_0"));
WebElement we =  myDriver.findElement(By.xpath( " //a[@id='newSearchNavigateHeadingEventId_01']  "));
WebElement we =  myDriver.findElement(By.xpath("//a[text()='Makerspace Docklands - Safety Induction']"));
WebElement we =  myDriver.findElement(By.xpath("//a[@href='book/event?eid=757231&amp;']")) ;

But I keep getting the following message...

org.openqa.selenium.NoSuchElementException: Unable to locate element: ....

Could some-one suggest what would be the correct path so that I can click on the link with:

we.click();

CodePudding user response:

To identify the element with text as Makerspace Docklands - Safety Induction you can use either of the following Locator Strategies:

  • cssSelector:

    WebElement we = driver.findElement(By.cssSelector("a.search-heading[id^='newSearchNavigateHeadingEventId']>h2.ng-binding[ng-bind*=EventDisplayName]"));
    
  • xpath:

    WebElement we = driver.findElement(By.xpath("//a[@class='search-heading' and starts-with(@id, 'newSearchNavigateHeadingEventId')]/h2[contains(., 'Makerspace Docklands') and contains(@ng-bind, 'EventDisplayName')]"));
    

However, as the element is a dynamic element so to identify the element you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • cssSelector:

    WebElement we = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.search-heading[id^='newSearchNavigateHeadingEventId']>h2.ng-binding[ng-bind*=EventDisplayName]")));
    
  • xpath:

    WebElement we = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='search-heading' and starts-with(@id, 'newSearchNavigateHeadingEventId')]/h2[contains(., 'Makerspace Docklands - Safety Induction') and contains(@ng-bind, 'EventDisplayName')]")));
    

Reference

You can find a detailed discussion on NoSuchElementException in:

CodePudding user response:

Thank you... I found this also worked for me...

WebElement we = myDriver.findElement (By.xpath ("//*[contains(text(),'Makerspace Docklands - Safety Induction')]"));
we.click();
  • Related