繁体   English   中英

无法识别 Selenium Webdriver 中的元素(skynews 上的 cookie 弹出窗口)

[英]Not able to identify an element in Selenium Webdriver (cookies pop-up on skynews)

我已经尝试了包括 xpath 在内的所有方法,但我仍然无法点击https://www.news.sky.com上 cookie 弹出窗口上的接受按钮

尝试了 css 选择器、xpath、frame 等一切。

这是我的代码:

公共类浏览器{

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();
}
}

请问有人可以帮我吗?

我已经在这个论坛和其他论坛上浏览了很多帖子,但找不到解决方案。

谢谢

我会建议//button[@title='Accept']的 xpath 的价值。

切换到 可能不起作用,因为该元素尚不存在于框架中。

    driver.switchTo().frame("sp_message_iframe_368417");
    
    WebDriverWait wait = new WebDriverWait(driver, 10000);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@title='Accept']")));        
    
    driver.findElement(By.xpath("//button[@title='Accept']")).click();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM