【问题标题】:Not able to identify an element in Selenium Webdriver (cookies pop-up on skynews)无法识别 Selenium Webdriver 中的元素(天空新闻上弹出 cookie)
【发布时间】: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


【解决方案1】:

我会建议 //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();

【讨论】:

  • 感谢您的回复。我在 WebDriverWait 上收到此警告 - “构造函数 WebDriverWait(WebDriver, long) 已弃用”,问题仍然存在。还有其他想法吗?
猜你喜欢
  • 2016-04-05
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
相关资源
最近更新 更多