3

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

1 Answer 1

5

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();
Sign up to request clarification or add additional context in comments.

8 Comments

sadly tried that and get the following exception: Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element
Sadly I get an exception with both. The last answer I get the following exception: Unable to locate element: {"method":"class name","selector":"oList"}
@JamesKing thanks, do you have any (i)frame elements on the page?
yep <iframe class="clsGrayborder" id="frmCountry" style="BACKGROUND: #e8e8e8" name="frmCountry" marginwidth="0" marginheight="0" src="pfa_Country.aspx" width="100%"></iframe>
@JamesKing okay, then, you need to switch to it before making an element search: driver.switch_to.frame("frmCountry"). Then, after you are done working inside the iframe, switch back to the default content: driver.switch_to.default_content().
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.