James King
James King

Reputation: 2445

Selenium - Click on link

I am trying to click on a link which is in a list. As can be seen from the screenshot I am trying to click on the "Algeria" link. How can I get there?

capture of list and elements

The Css is as follows - #\33 \2c ALG

xpath is - //*[@id="3,ALG"]

I have tried finding it by xpath and cssSelector but with no luck

Upvotes: 3

Views: 722

Answers (1)

alecxe
alecxe

Reputation: 474171

By.linkText() locator fits here perfectly:

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

You might also need to add an Explicit Wait to wait for element to be present:

WebDriverWait wait = new WebDriverWait(webDriver, 10);

WebElement link = wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Algeria"))); 
link.click();

You may also need to open up the list before clicking the link:

WebDriverWait wait = new WebDriverWait(webDriver, 10);

WebElement linkList = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("oList"))); 
linkList.click();

WebElement link = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Algeria"))); 
link.click();

Upvotes: 5

Related Questions