【发布时间】:2020-10-28 01:58:29
【问题描述】:
我已经尝试了包括 xpath 在内的所有方法,但我仍然无法点击 https://www.news.sky.com 上 cookie 弹出窗口上的接受按钮
尝试了 css 选择器、xpath、框架等一切。
这是我的代码:
公共类浏览器{
WebDriver driver;
public void browser_open() {
String projectPath = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", projectPath+"\\Drivers\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
}
public void navigate() throws InterruptedException {
driver.get("http://news.sky.com");
//Thread.sleep(5000);
driver.switchTo().frame("sp_message_iframe_368417");
driver.findElement(By.xpath("/html/body/div/div[3]/div[3]/button[1]")).click();
}
}
请问有人可以帮我解决这个问题吗?
我已经在这个论坛和其他论坛上浏览了很多帖子,但找不到解决方案。
谢谢
【问题讨论】:
-
有错误吗?
-
使用相对 xpath 而不是绝对 xpath。尝试将其用于按钮 xpath //button[text()='Accept'] 并删除与切换框架相关的代码。
-
如果我删除了与开关框架相关的代码。我收到错误“org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//button[@title='Accept']”}”但是,如果我离开它,那么它会在 30-40 秒后起作用。
标签: java selenium xpath webdriver