Java 无法识别Selenium Webdriver中的元素(skynews上弹出的cookies)
我已经尝试了包括xpath在内的所有方法,但仍然无法单击cookies弹出窗口上的Accept按钮 尝试了css选择器、xpath、框架等一切 这是我的密码: 公共类浏览器{Java 无法识别Selenium Webdriver中的元素(skynews上弹出的cookies),java,selenium,xpath,webdriver,Java,Selenium,Xpath,Webdriver,我已经尝试了包括xpath在内的所有方法,但仍然无法单击cookies弹出窗口上的Accept按钮 尝试了css选择器、xpath、框架等一切 这是我的密码: 公共类浏览器{ WebDriver driver; public void browser_open() { String projectPath = System.getProperty("user.dir"); System.setProperty("webdriver.chr
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();
}
}
有人能帮我吗
我已经在这个论坛和其他论坛上浏览了很多帖子,但找不到解决方案
谢谢我建议使用
//按钮[@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();
是否有错误?请使用相对xpath而不是绝对xpath。尝试对按钮xpath//button[text()='Accept']使用此选项,并删除与开关框架相关的代码。如果删除与开关框架相关的代码,则会出现错误“org.openqa.selenium.NoSuchElementException:无此类元素:无法找到元素:{“方法”:“xpath”,“选择器”:”//按钮[@title='Accept']“}”但是,如果我离开它,它将在30-40秒后工作。感谢您的回复。我在WebDriverWait上收到此警告-“构造函数WebDriverWait(WebDriver,long)已被弃用”,问题仍然存在。还有其他想法吗?