0
votes

HTML:

<button type="button" class="modal-footer-button g-capitalize btn btn link">Cancel</button>

Code trial:

By.xpath("//button[@type='button']").click()

didn't work.

Tried number of other ways too. Unable to click on Cancel button. error:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@type='button'][@class='modal-footer-button g-capitalize btn btn-link'][@value='Cancel']"}

3
What error you are getting? Share relevant HTML and exact code you tried. - Shivam Mishra
Is the button inside <frame> tag? - Guy
Selector in provided code differs from the one in Exception log... Both of them didn't work? - Andersson

3 Answers

0
votes

This might be a timing issue. Try to wait until required button appears in DOM:

WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='fade modal' and @role='dialog']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(@class, 'modal-footer-button') and text()='Cancel']"))).click();
0
votes

Try this

WebElement Cancelbtn= driver.findElement(By.xpath("//button[text()='Cancel']"));
Cancelbtn.click();

Also, place some wait till element to be located

driver.manage().window().maximize();    
WebElement Cancelbtn= driver.findElement(By.xpath("//button[text()='Cancel']"));
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(Cancelbtn));
Cancelbtn.click();
0
votes

As per the HTML you have shared the element with text as Cancel is within a Modal Window so you have to induce WebDriverWait for the desired element to be clickable and you can use either of the following solution:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.modal-footer-button.g-capitalize.btn.btn.link[type='button']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='modal-footer-button g-capitalize btn btn link' and contains(.,'Cancel')]"))).click();