boopathi
boopathi

Reputation: 87

How to click a link with selenium Webdriver

How to click a link with selenium Webdriver which have same names

driver.findElement(By.linkText("View All")).click();

There are some other Links also having same name like View All

Upvotes: 0

Views: 868

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23845

You should try to locate unique link with combinations of other attribute of this link such as class name and text by using xpath as below :-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement link = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//a[contains(@class, 'absence-viewall') and contains(text(), 'View All')]")));
link.click();

Or if this link has unique class name, best way to use By.cssSelector() as below :-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement link = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a.absence-viewall")));
link.click();

Upvotes: 2

Related Questions