我的代碼只要打開一個瀏覽器窗口並瀏覽到一個URL即可。我的代碼中沒有發現任何錯誤。我需要處理由瀏覽器生成的事件,特別是當用戶關閉瀏覽器窗口時。沒有任何消息在我的OverrideClass中被捕獲。這段代碼爲什麼不處理來自瀏覽器的事件?
我有這樣的:
try {
System.setProperty("webdriver.gecko.driver", "./geckodriver.exe");
driver = new FirefoxDriver();
EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
OverrideClass oc = new OverrideClass();
eventDriver.register(oc);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} catch (Exception ex) {
Log.logError("Browser.initBrowser(): " + ex.getLocalizedMessage());
}
消息處理程序是這樣的:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
public class OverrideClass implements WebDriverEventListener{
// @Override
// public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
//
// }
@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("OverrideClass.afterClickOn(): " + arg0.toString());
}
@Override
public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
System.out.println("OverrideClass.afterFindBy(): " + arg0.toString()); }
@Override
public void afterNavigateBack(WebDriver driver) {
System.out.println("After clicking back "+ driver.getCurrentUrl());
}
@Override
public void afterNavigateForward(WebDriver arg0) {
}
@Override
public void afterNavigateTo(String arg0, WebDriver arg1) {
}
@Override
public void afterScript(String arg0, WebDriver arg1) {
}
// @Override
// public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
//
// }
@Override
public void beforeClickOn(WebElement arg0, WebDriver arg1) {
}
@Override
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
}
@Override
public void beforeNavigateBack(WebDriver driver) {
System.out.println("OverrideClass.beforeNavigateBack() Before clicking back: " + driver.getCurrentUrl());
}
@Override
public void beforeNavigateForward(WebDriver arg0) {
System.out.println("OverrideClass.beforeNavigateForward() Before clicking back: " + arg0.getCurrentUrl());
}
@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {
System.out.println("OverrideClass.beforeNavigateTo(): " + arg0);
}
@Override
public void beforeScript(String arg0, WebDriver arg1) {
}
@Override
public void onException(Throwable arg0, WebDriver arg1) {
System.out.println("OverrideClass.onException(): " + arg0.getLocalizedMessage());
}
@Override
public void afterAlertAccept(WebDriver arg0) {
}
@Override
public void afterAlertDismiss(WebDriver arg0) {
}
@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
}
@Override
public void afterNavigateRefresh(WebDriver arg0) {
}
@Override
public void beforeAlertAccept(WebDriver arg0) {
}
@Override
public void beforeAlertDismiss(WebDriver arg0) {
}
@Override
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
}
@Override
public void beforeNavigateRefresh(WebDriver arg0) {
}
}
你通過'eventDriver'加載頁面?看起來你仍然使用原始的'driver'('driver.manage()。timeouts()')而不是'eventDriver'包裝器。 –
EventFiringWebDriver用於偵聽通過該驅動程序上的代碼啓動的事件,而不是用戶生成的事件。如果用戶更改瀏覽器的URL,則不會在您的代碼上觸發事件。因此,只有當您對'eventDriver'對象採取行動時,纔會觸發此事件。 –
@VinceEmigh謝謝!接得好。 – nicomp