这是我在Web驱动程序的每个页面上执行java脚本的代码,并根据我的脚本从cookie中获取值。它工作正常。但问题是,如果页面刷新它无法正常工作,因为我使用当前URL检查以前的URL。如何处理页面的刷新。帮助我
String s="Some javascript";
String prev = driver.getTitle();
((JavascriptExecutor) driver).executeScript(s);
iflag = 0;
Boolean record = Boolean.valueOf(true);
while (record.booleanValue()) {
try {
curr = driver.getTitle();
//Check if page changed or not
///help needed here
if (!(prev.equals(curr))) {
System.out.println("changes");
((JavascriptExecutor) driver).executeScript(s);
prev = curr;
}
Set<String> wnd_hndls = driver.getWindowHandles();
Iterator wnd_itr = wnd_hndls.iterator();
if (!driver.getCurrentUrl().equalsIgnoreCase("about:blank")) {
int win_ind = 0;
while (wnd_itr.hasNext()) {
String wnd_hndlr = (String) wnd_itr.next();
win_ind++;
Cookie ck_pg = driver.manage().getCookieNamed(wnd_hndlr);
if ((ck_pg != null) && (ck_pg.getValue().equalsIgnoreCase("0")))
{
iflag = 1;
}
if (ck_pg == null)
{
iflag = 1;
}
if (iflag == 1) {
//My operation based on executed javascript
}
}
}
} catch (WebDriverException localWebDriverException) {
driver.quit();
record = Boolean.valueOf(false);
}
}
答案 0 :(得分:0)
根据各种因素更改页面可能需要一些时间。您应该等待睡眠新页面上的元素,直到它发生变化。请尝试以下方法:
//To check if page changed or not use this instead of simple driver.getTitle()
int i = 0;
do{
Thread.sleep(500);// Or wait for element on new page.
curr = driver.getTitle();
i++;
}while(prev.equals(curr)&&i<10);
if (!(prev.equals(curr))) {
System.out.println("changes");
((JavascriptExecutor) driver).executeScript(s);
prev = curr;
}
要刷新页面使用,请执行以下操作:
driver.navigate().refresh();