简体   繁体   中英

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.

码

I need identify the modal and click in the button.

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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM