Renata Barros
Renata Barros

Reputation: 41

How to click on the button with text as Close within a modal dialog box as per the html through Selenium and C#

I am using Selenium Webdriver using C#, but selenium isn't identifying my Modal Window.

I have tried:

 IAlert alert = driver.SwitchTo().Alert();
 alert.Accept();

But it doesn't work. In the code the modal has 'ID' but, it doesn't work either.

Code

I need identify the modal and click in the button.

Upvotes: 2

Views: 855

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

As per the HTML you have shared to click on the button with text as Close you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:

  • Using CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("button.close[data-dismiss='modal'][aria-label='Close']"))).Click();   
    
  • Using XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[@class='close' and @data-dismiss='modal'][@aria-label='Close']"))).Click();
    

Upvotes: 1

Related Questions