While trying to navigate all the pages on a website, I encountered an issue where after scrolling to the second page, I received an exception stating that the element does not exist on the page. Upon further investigation, I realized that the CSS selector for the elements changed at different points in the website's structure. Sometimes it appears as
WebElement nextButton = driver.findElement(By.cssSelector("#pagination-head>a>div>span.hidden-xs.hidden-sm"));
, while other times it is WebElement nextButton = driver.findElement(By.cssSelector("#pagination-head > a:nth-child(3) > div > span.hidden-xs.hidden-sm"));
. My question is, how can I handle this scenario in Selenium? I looked into methods to check if a given WebElement exists or not, but unfortunately, I couldn't find any :/