1

I have tried this

List <WebElement> navlist = driver.findElements(By.cssSelector("d-md-down-none nav navbar-nav mr-auto"));
navlist.get(0).findElement(By.linkText("Sources")).click();

Below is the HTML code:

<ul class="d-md-down-none nav navbar-nav mr-auto">
    <li class="px-3 nav-item">
        <a aria-disabled="false" href="#/sources" class="nav-link">Sources</a>
    </li>
    <li class="px-3 nav-item">
        <a aria-disabled="false" href="#/alerts" class="nav-link">Alerts</a>
    </li>
</ul>

Error when trying my example :

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at Adding_new_source.New_source.main(New_source.java:53) 

How to click in nav-bar and choose item from a list in selenium java

JeffC
  • 22,180
  • 5
  • 32
  • 55
Shams
  • 43
  • 9

2 Answers2

1

As per the HTML you have provided and your code trials you can choose and click an item with text as Sources from the List using the following code block :

List <WebElement> navlist = driver.findElements(By.cssSelector("ul.d-md-down-none.nav.navbar-nav.mr-auto li>a"));
for(WebElement elem:navlist)
    if(elem.getAttribute("innerHTML").contains("Sources"))
        {
            elem.click();
            break;
        }
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • tried the above, error message:no such element found. – Shams May 07 '18 at 12:21
  • For _no such element found_ see this discussion [NoSuchElementExeption, selenium unable to locate element](https://stackoverflow.com/questions/48471321/nosuchelementexeption-selenium-unable-to-locate-element/48472940#48472940) – undetected Selenium May 07 '18 at 12:28
  • i fixed my code, now i used the above code to try and target the specific "Source" element, in my console there is no error message so i did a print out of where its breaking and its breaking just after i insert the above code. "error on source selection" – Shams May 07 '18 at 14:28
  • When are you encountering `error on source selection` ? Did you click on the Dropdown for the `
      ` to expand so you would be able to create the `List`?
    – undetected Selenium May 07 '18 at 14:30
  • yes i have, could it be that its because the list is also in a nav-bar? – Shams May 08 '18 at 08:19
  • href="#/dashboard" class="navbar-brand active" target="_self"> – Shams May 08 '18 at 08:20
0

You are getting the error because your CSS selector is incorrect. You have listed the class names but classes should be preceeded with a ., e.g. .className. The equivalent of your code would be

List <WebElement> navlist = driver.findElements(By.cssSelector(".d-md-down-none.nav.navbar-nav.mr-auto"));
navlist.get(0).findElement(By.linkText("Sources")).click();

Have you tried the simpler

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

It may or may not work depending on how many other "Sources" links there are on the page and where they are.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • What does "doesn't work" mean? What error message do you get or what happens? What about the other way I posted? – JeffC May 08 '18 at 12:54
  • What about the first method I posted? What was the result there? Are you sure that these elements aren't inside an `IFRAME`? – JeffC May 08 '18 at 13:00
  • its in a plain html body no iframes, although before i get to this url there is another screen authentication which has a iframe, which i have already got into and now im coming out of it into this new HTML body but it has no iframes. i tried the first method its also gives me a "no such element error" – Shams May 08 '18 at 13:06
  • this is my code before i use your code just to give you context of the previous iframe> driver.findElement(By.id("login-button")).click(); driver.switchTo().frame(driver.findElement(By.id("duo_iframe")));; WebDriverWait wait = new WebDriverWait(driver, 10); WebElement button = wait.until (ExpectedConditions.elementToBeClickable(By.cssSelector (".positive.auth-button[type='submit']"))); button.click(); – Shams May 08 '18 at 13:08
  • Did you ever execute `driver.switchTo().defaultContent()`? That may be the issue. Add that code and any other details to your question so that everyone can see. – JeffC May 08 '18 at 14:18