Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 909
James King Avatar asked Dec 21 '25 20:12

James King


1 Answers

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();
like image 147
alecxe Avatar answered Dec 23 '25 12:12

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!