Good day.
On the Google results page for the query "habrahabr.ru" I want to click on the result of the habr.
Option "Click the first result" is not suitable.
Trying to write so that the desired link is pressed at any position in the search results.
The clickLinkByHref () method shown below results in
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: The element reference is stale. Either the page has been refreshed.
public static void clickLinkByHref() { List<WebElement> links = driver.findElements(By.tagName("a")); for ( WebElement link: links) { if(link.getAttribute("href").contains("habrahabr.ru")) { link.click(); break; } } }; On the stack there was a solution to try to knock on the link through while, until selenium sees it:
public static void clickLinkByHref() { List<WebElement> links = driver.findElements(By.tagName("a")); for (WebElement link : links) { if(link.getAttribute("href").contains("habrahabr.ru")) { int attempts = 0; while(attempts < 5) { try { link.click(); break; } catch(StaleElementException e) {} } } } }; As a result, he buried himself in this and got stuck:
StaleElementException cannot be resolved to a type
Help me please. thank