user3765899 user3765899 - 2 years ago 116
Java Question

Selenium cannot find item list input

Hello I'm learning selenium and I've met first problem. I was searching for it very long and tested a lot of solutions and none of them worked for me.

This is how looks button which I want to click (I want to click Events button in navbar)

<div id="navbar-items" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="#about">About</a></li>
<li><a href="#events">Events</a></li>
<li><a href="#venues">Venues</a></li>
<li><a href="#bookings">Bookings</a></li>
<li><a href="#monitor">Monitor</a></li>
<li><a href="admin">Administration</a></li>
</ul>
</div>


And here is my java code:

public class SeleniumTest {

@Test
public void startWebDriver() throws InterruptedException{

WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://localhost:8080/ticket-monster/");

Assert.assertTrue("title should start differently",
driver.getTitle().startsWith("Ticket Monster"));

Thread.sleep(7000);

driver.findElement(By.xpath("//div[@id='navbar-items'][2]")).click();

//*[@id="navbar-items"]/ul/li[2]/a

// I also was trying this and nothing clicked events button
//driver.findElement(By.xpath("(//span[@class='nav navbar-nav'])2]")).click();
//driver.findElement(By.cssSelector("ul > li:nth-child(2)")).click();
}
}


Here is website on which on I am practising it may be helpful.
http://ticketmonster-jdf.rhcloud.com/

I will be very thankful for every help.

Answer Source

Try the below code and let me know.

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("example.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='navbar-items']/ul/li[2]/a")));
driver.findElement(By.xpath(".//*[@id='navbar-items']/ul/li[2]/a")).click();

And Xpath is the slowest, so xpath is used when we don't have any option. Fortunately the xpath is taking the id so here it is all good.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download