Issue
What is the wrong in my code , why auto click doesn't work in accept cookies button. This website using angular application.
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.paulhammant.ngwebdriver.ByAngular;
import com.paulhammant.ngwebdriver.NgWebDriver;
public class NewClass {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Hp\\Downloads\\chromedriver_win32\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
NgWebDriver ngWebDriver = new NgWebDriver(driver);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://visa.vfsglobal.com/ind/en/deu/login/");
ngWebDriver = new NgWebDriver((JavascriptExecutor) driver);
ngWebDriver.waitForAngularRequestsToFinish();
driver.findElement(ByAngular.options("onetrust-accept-btn-handler")).click();
}
}
Solution
When you use this line
driver.findElement(ByAngular.options("onetrust-accept-btn-handler")).click();
it will try to find the element immediately, often resulting in errors. Cause web element/elements have not been rendered properly.
This is main reason we should opt for Explicit waits, implemented by WebDriverWait.
They allow your code to halt program execution, or freeze the thread, until the condition you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting.
Code :
Webdriver driver = new ChromeDriver();
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("onetrust-accept-btn-handler"))).click();
Answered By - cruisepandey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.