user 007
user 007

Reputation: 11

how to locate an element to perform a click

I'm trying to navigate in site: http://startupnationbook.com/startup-map I want to click on link of Startups, however I'm not able to locate the element.

I tried:

elem = driver.findElement(By.xpath("//a[contains(text(),'Startups')]"));

and

elem = driver.findElement(By.xpath("//*[@id='listtoggle' and contains(text(),'Startups')]"));

In both cases I get: "Unable to locate element" error

What is wrong in my expressions and how can I locate the element to perform a click.

Upvotes: 0

Views: 78

Answers (2)

Helping Hands
Helping Hands

Reputation: 5396

You can do like below :

WebDriver driver = new FirefoxDriver();
driver.get("http://startupnationbook.com/");
driver.findElement(By.xpath(".//*[@id='main-nav']/li[5]/a")).click();

Hope this will work. I have tested.

Upvotes: 1

Andrew Bocz Jr
Andrew Bocz Jr

Reputation: 1005

The map is in an iframe, you're probably not telling Selenium to look in there. I would also look for the span tag, and since the id listtoggle is used many times (poor design, making it worthless), just look for contains Startups.

// Also should probably use a wait here, in case the page takes too long to load
chromeDriver.switchTo().frame(chromeDriver.findElement(By.tagName("iframe")));
WebElement elem = chromeDriver.findElement(By.xpath("//span[contains(text(),'Startups')]")); 

Upvotes: 2

Related Questions