In order to close a random pop up on my web page, I followed a tutorial of wrapping web elements with a proxy wrapper: https://www.vinsguru.com/selenium-webdriver-how-to-handle-annoying-random-popup-alerts/
After implementing this solution in my code, the random pop up got closed but I encountered 'no such element exception' which I didn't have previously. I tried to change the line of the code by using different elements and methods but non of my actions resolved this problem - non of the elements were recognized .
I will appreciate any help :)
The method which causing the exception (the last line):
private void search(String gameName) {
Wait.visible(homeBtn);
Methods.click(searchBtn);
Methods.sendKeys(searchBox, gameName);
Wait.visible(searchResultsTitle);
}
proxy page factory class:
public class ProxyPageFactory {
public static <T> void initElements(WebDriver driver, T pageobject){
//first init elements
PageFactory.initElements(driver, pageobject);
//then access all the WebElements and create a wrapper
for(Field f: pageobject.getClass().getDeclaredFields()){
if(f.getType().equals(WebElement.class)){
boolean accessible = f.isAccessible();
f.setAccessible(true);
//reset the webelement with proxy object
try {
f.set(pageobject, ElementGuard.guard((WebElement)f.get(pageobject)));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
f.setAccessible(accessible);
}
}
}
element guard class:
public class ElementGuard {
public static WebElement guard(WebElement element) {
ElementProxy proxy = new ElementProxy(element);
WebElement wrappdElement = (WebElement) Proxy.newProxyInstance(ElementProxy.class.getClassLoader(),
new Class[] { WebElement.class },
proxy);
return wrappdElement;
}
element proxy class:
public class ElementProxy implements InvocationHandler {
private final WebElement element;
public ElementProxy(WebElement element) {
this.element = element;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//before invoking actual method check for the popup
this.checkForPopupAndKill();
return (method.invoke(element, args));
}
private void checkForPopupAndKill() {
// closing the pop up
} }
the error:
java.lang.reflect.UndeclaredThrowableException at com.sun.proxy.$Proxy27.isDisplayed(Unknown Source) at org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:314) at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:43) at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:300) at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:297) at org.openqa.selenium.support.ui.FluentWait.lambda$checkConditionInLoop$2(FluentWait.java:233) at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1764) at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1756) at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1016) at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1665) at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1598) at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183) at ✽.I perform an action to invoke the block(file:///C:/Users/may/Desktop/automation-git/GTH-QA/cucumber/features/Blocks.feature:9) Caused by: java.lang.reflect.InvocationTargetException at jdk.internal.reflect.GeneratedMethodAccessor6.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at infrastructure.ElementProxy.invoke(ElementProxy.java:27) at com.sun.proxy.$Proxy27.isDisplayed(Unknown Source) at org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:314) at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:43) at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:300) at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:297) at org.openqa.selenium.support.ui.FluentWait.lambda$checkConditionInLoop$2(FluentWait.java:233) at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1764) at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1756) at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1016) at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1665) at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1598) at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183) Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(text(),'SEARCH RESULTS')]"} (Session info: chrome=86.0.4240.111) For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element *** Element info: {Using=xpath, value=//div[contains(text(),'SEARCH RESULTS')]} at jdk.internal.reflect.GeneratedConstructorAccessor15.newInstance(Unknown Source) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:196) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:129) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:160) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:582) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:333) at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:451) at org.openqa.selenium.By$ByXPath.findElement(By.java:394) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:325) at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:70) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:39) at com.sun.proxy.$Proxy25.isDisplayed(Unknown Source) ... 17 more