org.openqa.selenium.NoSuchElementException: Unable to find element with locator By.xpath : issue unresolvable

61 Views Asked by At

I have been trying to click on this button Accept all cookies on the website: https://smartenergycodecompany.co.uk/the-smart-energy-code-2/

It just doesn't seem to work. I have changed XPath, ClassName and tried to use CSSSelector. This is the button class

<button class="cmplz-btn cmplz-accept cc-allow cc-btn">Accept all cookies</button> 

I am going to the website by clicking on testing documentation from the website: https://www.smartdcc.co.uk/our-smart-network/network-products-services/testing-services/. This works fine as seen in the picture and also can be seen from my step definition. The click is working for the step after where the element is not found on smart energy to click on the button.

public class DccSteps {

    
    WebDriver driver = new ChromeDriver();
    
@Given("the browser is open")
public void the_browser_is_open() {
    System.out.println("Inside step - browser is open");
    
    
}

@And("the user has access to navigate to smartdcc.com")
public void the_user_has_access_to_navigate_to_smartdcc_com() {
    System.out.println("Inside step - The user has access to a smartdcc page");
    driver.navigate().to("https://www.smartdcc.co.uk/");
    driver.manage().window().maximize();

    
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
    driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60));
    
    JavascriptExecutor js=(JavascriptExecutor)driver;
    
    WebElement checkbox=driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[4]/div[2]/div/button"));
    
    js.executeScript("arguments[0].click()", checkbox);
    //js.executeScript("document.getElementsByClassName('optanon-allow-all accept-cookies-button').click()");

    
    //driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[4]/div[2]/div/button")).click();
}

@When("the user clicks on about DCC they should be on the correct page")
public void the_user_clicks_on_about_dcc_they_should_be_on_the_correct_page() {
    driver.findElement(By.xpath("//nav[@class='primary-navigation primary-navigation--desktop']//span[contains(text(),'About DCC')]")).click();
    driver.getPageSource().contains("Enabling innovation for public good");
    System.out.println("Inside step - User clicks on the right page");
}

@Then("the user clicks on enabling innovation")
public void the_user_clicks_on_enabling_innovation() {
    System.out.println("Inside step - User clicks on link");
    driver.findElement(By.xpath("/html/body/header/div[1]/div/div/div[2]/nav/ul/li[3]/a")).click();
}

@When("the user scrolls down they should see testing services")
public void the_user_scrolls_down_they_should_see_testing_services() {
    System.out.println("Inside step - Testing services should be visible");
    driver.getPageSource().contains("Testing Services");
}

@Then("the user will click on the testing services")
public void the_user_will_click_on_the_testing_services() {
    System.out.println("Inside step - user will click on the link");
    driver.findElement(By.xpath("/html/body/main/div[2]/div/div[10]/div/div[2]/div/div[1]/div/a/div[2]/h2")).click();
    
}

@Then("the user should be on the testing page")
public void the_user_should_be_on_the_testing_page() {
    System.out.println("Inside step - User should be be on the testing page");
    driver.getPageSource().contains("Testing you can trust");
}


    
@When("the user clicks on the testing documentation")
public void the_user_clicks_on_the_testing_documentation() {
    System.out.println("Inside step - User clicks and sees different testing docs");
    
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
    driver.findElement(By.xpath("//a[@data-text='Testing documentation']")).click();

which can be seen on the image enter image description here I have looked at documentation but nothing has helped with the button being clicked.

I have tried making different changes using various methods but the button is not being clicked at all.

1

There are 1 best solutions below

4
Shawn On

Refer below working code: It uses Explicit Waits and XPath locator strategy to locate and perform action on Accept all cookies button.

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://smartenergycodecompany.co.uk/the-smart-energy-code-2/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Accept all cookies']"))).click();

UPDATE:

driver.findElement(By.xpath("//a[@data-text='Testing documentation']")).click();
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));      
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Accept all cookies']"))).click();

// Use below line if you want to switch back to previous tab
//driver.switchTo().window(tabs.get(0));