org.openqa.selenium.NoSuchElementException:无法使用定位器 By.xpath 找到元素:问题无法解决

问题描述 投票:0回答:1

我一直在尝试单击此按钮接受网站上的所有cookiehttps://smartenergycodecompany.co.uk/the-smart-energy-code-2/

这似乎不起作用。我已经更改了 XPath、ClassName 并尝试使用 CSSSelector。这是按钮类

<button class="cmplz-btn cmplz-accept cc-allow cc-btn">Accept all cookies</button> 

我将通过以下网站点击测试文档访问该网站:https://www.smartdcc.co.uk/our-smart-network/network-products-services/testing-services/。如图所示,这工作得很好,也可以从我的步骤定义中看出。单击适用于在智能能源上找不到该元素以单击按钮之后的步骤。

可以在图像上看到 enter image description here 我查看了文档,但没有任何内容对单击按钮有帮助。

我尝试使用各种方法进行不同的更改,但根本没有单击该按钮。

java selenium-webdriver selenium-chromedriver buttonclick
1个回答
0
投票

请参阅下面的工作代码:它使用显式等待和XPath定位器策略来定位接受所有cookie按钮并执行操作。

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://smartenergycodecompany.co.uk/the-smart-energy-code-2/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Accept all cookies']"))).click();
© www.soinside.com 2019 - 2024. All rights reserved.