Selenium遇NoSuchElementException:无法定位点击Accept all cookies按钮
问题场景
- 从指定页面点击testing documentation进入目标网站后,无法定位并点击Accept all cookies按钮,报错:
org.openqa.selenium.NoSuchElementException: Unable to find element with locator By.xpath : issue unresolvable - 已尝试更换XPath、ClassName、CSSSelector,均未解决问题
目标按钮HTML代码
<button class="cmplz-btn cmplz-accept cc-allow cc-btn">Accept all cookies</button>
操作流程
- 打开浏览器,访问初始网站
- 处理初始网站的Cookie授权
- 依次点击"About DCC"、"Enabling innovation",滚动到"Testing Services"并点击进入对应页面
- 点击"testing documentation"跳转至目标网站,尝试点击Cookie按钮失败
测试代码片段
public class DccSteps { WebDriver driver = new ChromeDriver(); @Given("the browser is open") public void the_browser_is_open() { System.out.println("Inside step - browser is open"); } @And("the user has access to navigate to smartdcc.com") public void the_user_has_access_to_navigate_to_smartdcc_com() { System.out.println("Inside step - The user has access to a smartdcc page"); driver.navigate().to("https://www.smartdcc.co.uk/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60)); JavascriptExecutor js=(JavascriptExecutor)driver; WebElement checkbox=driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[4]/div[2]/div/button")); js.executeScript("arguments[0].click()", checkbox); //js.executeScript("document.getElementsByClassName('optanon-allow-all accept-cookies-button').click()"); //driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[4]/div[2]/div/button")).click(); } @When("the user clicks on about DCC they should be on the correct page") public void the_user_clicks_on_about_dcc_they_should_be_on_the_correct_page() { driver.findElement(By.xpath("//nav[@class='primary-navigation primary-navigation--desktop']//span[contains(text(),'About DCC')]")).click(); driver.getPageSource().contains("Enabling innovation for public good"); System.out.println("Inside step - User clicks on the right page"); } @Then("the user clicks on enabling innovation") public void the_user_clicks_on_enabling_innovation() { System.out.println("Inside step - User clicks on link"); driver.findElement(By.xpath("/html/body/header/div[1]/div/div/div[2]/nav/ul/li[3]/a")).click(); } @When("the user scrolls down they should see testing services") public void the_user_scrolls_down_they_should_see_testing_services() { System.out.println("Inside step - Testing services should be visible"); driver.getPageSource().contains("Testing Services"); } @Then("the user will click on the testing services") public void the_user_will_click_on_the_testing_services() { System.out.println("Inside step - user will click on the link"); driver.findElement(By.xpath("/html/body/main/div[2]/div/div[10]/div/div[2]/div/div[1]/div/a/div[2]/h2")).click(); } @Then("the user should be on the testing page") public void the_user_should_be_on_the_testing_page() { System.out.println("Inside step - User should be be on the testing page"); driver.getPageSource().contains("Testing you can trust"); } @When("the user clicks on the testing documentation") public void the_user_clicks_on_the_testing_documentation() { System.out.println("Inside step - User clicks and sees different testing docs"); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); driver.findElement(By.xpath("//a[@data-text='Testing documentation']")).click();
解决方案建议
- 检查iframe嵌套:Cookie弹窗可能处于iframe内,需先切换到对应iframe再定位元素:
// 替换为实际iframe的id/name或索引 driver.switchTo().frame("cookie-iframe"); // 定位并点击按钮 driver.switchTo().defaultContent(); // 操作完成后切回主文档 - 改用显式等待:隐式等待在页面跳转后可能失效,用显式等待确保元素可交互:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30)); WebElement acceptBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.cmplz-accept"))); acceptBtn.click(); - 使用精准定位器:优先选择具备唯一特征的定位方式:
- CSSSelector:
button.cmplz-btn.cmplz-accept - XPath:
//button[contains(@class,'cmplz-accept') and text()='Accept all cookies']
- CSSSelector:
- 切换到新窗口:点击"testing documentation"可能打开新标签页,需切换到目标窗口:
Set<String> windowHandles = driver.getWindowHandles(); for (String handle : windowHandles) { driver.switchTo().window(handle); if (driver.getCurrentUrl().contains("smartenergycodecompany")) { break; } }
内容的提问来源于stack exchange,提问作者Maha
相关产品推荐
相关产品推荐

