Java Selenium句柄页面刷新
下面是我的代码,用于在web驱动程序的每个页面上执行java脚本,并根据脚本从Cookie中获取值。它很好用。但问题是,如果页面被刷新,它将不工作,因为我用当前URL检查了以前的URL。如何处理页面刷新。请帮助我Java Selenium句柄页面刷新,java,selenium,Java,Selenium,下面是我的代码,用于在web驱动程序的每个页面上执行java脚本,并根据脚本从Cookie中获取值。它很好用。但问题是,如果页面被刷新,它将不工作,因为我用当前URL检查了以前的URL。如何处理页面刷新。请帮助我 String s="Some javascript"; String prev = driver.getTitle(); ((JavascriptExecutor) driver).executeScript(s);
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);
}
}
String s=“一些javascript”;
String prev=driver.getTitle();
((JavascriptExecutor)驱动程序);
iflag=0;
布尔记录=Boolean.valueOf(真);
while(record.booleanValue()){
试一试{
curr=driver.getTitle();
//检查页面是否已更改
///这里需要帮助
如果(!(上一个等于(当前))){
系统输出打印项次(“变更”);
((JavascriptExecutor)驱动程序);
上一次=当前;
}
设置wnd_hndls=driver.getWindowHandles();
迭代器wnd_itr=wnd_hndls.Iterator();
if(!driver.getCurrentUrl().equalsIgnoreCase(“关于:空白”)){
int win_ind=0;
while(wnd_itr.hasNext()){
字符串wnd_hndlr=(字符串)wnd_itr.next();
win_ind++;
Cookie ck_pg=driver.manage().getCookieNamed(wnd_hndlr);
if((ck_pg!=null)和&(ck_pg.getValue().equalsIgnoreCase(“0”))
{
iflag=1;
}
如果(ck_pg==null)
{
iflag=1;
}
如果(iflag==1){
//我的操作基于已执行的javascript
}
}
}
}捕获(WebDriverException localWebDriverException){
driver.quit();
记录=Boolean.valueOf(false);
}
}
根据各种因素,更改页面可能需要时间。您应该等待sleep的新页面上的元素,直到它发生更改。请尝试以下操作:
//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();
我不使用driver.navigate.refresh进行刷新,而是使用refresh按钮或f5按钮。这些页面是动态页面,因此等待页面上的元素在我的情况下不起作用。例如,首先我转到google(脚本注入),然后输入一些值并导航到其他页面(脚本再次注入)因为它是新的页面,但如果我点击刷新脚本将不会注入。我想要一种解决方法,你不需要使用它,我刚才提到它,因为刷新页面是你问题的一部分。如果其他部分对你有帮助,一定要接受答案。