While working on alerts Unable to click on a link

54 Views Asked by At
System.setProperty("webdriver.edge.driver",  "C:\\Users\\xxxx\\eclipseworkspace\\selenium\\msedgedriver.exe");
WebDriver driver = new EdgeDriver();

driver.get("https://www.selenium.dev/documentation/webdriver/interactions/alerts");

WebElement Okcancel = driver.findElement(By.xpath("/html/body/div/div[1]/div/main/div/p[5]/a"));
 Okcancel.click();

The above code snippet where issue is exists.

Website Link : https://www.selenium.dev/documentation/webdriver/interactions/alerts/

ERROR MSG:

at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:134)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:51)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:190)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:216)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:174)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:519)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:223)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:76)
at selenium.PopUp.main(PopUp.java:27)

I need a solution, for this what needs to be done to resolve this error.

1

There are 1 best solutions below

0
On

The link which you are trying to click is not displayed fully in the current context of the screen. So you can try to click on the link in multiple ways as below.

By using the Actions class

WebElement Okcancel = driver.findElement(By.xpath("/html/body/div/div[1]/div/main/div/p[5]/a"));
Actions action = new Actions(driver);
action.moveToElement(Okcancel).click().build().perform();

By using JavascriptExecutor

WebElement Okcancel = driver.findElement(By.xpath("/html/body/div/div[1]/div/main/div/p[5]/a"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", Okcancel);

By Using ChromeOptions/EdgeOptions you can zoom out the screen so that the link gets displayed

EdgeOptions options = new EdgeOptions();
options.addArguments("force-device-scale-factor=0.75");
WebDriver driver = new EdgeDriver(options);
WebElement Okcancel = driver.findElement(By.xpath("/html/body/div/div[1]/div/main/div/p[5]/a"));
Okcancel.click();