简体   繁体   English

如何通过Selenium和C#根据HTML在模态对话框中单击文本为“关闭”的按钮

[英]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. 我正在使用C#使用Selenium Webdriver,但是selenium无法识别我的模态窗口。

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. 在代码中,模式具有“ ID”,但是也不起作用。

码

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: 根据您共享的HTML ,单击带有“ 关闭”文本的按钮,您需要诱使WebDriverWait使所需元素可单击,并且可以使用以下任一解决方案:

  • Using CssSelector : 使用CssSelector

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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