簡體   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#

我正在使用C#使用Selenium Webdriver,但是selenium無法識別我的模態窗口。

我努力了:

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

但這是行不通的。 在代碼中,模式具有“ ID”,但是也不起作用。

碼

我需要確定模態,然后單擊按鈕。

根據您共享的HTML ,單擊帶有“ 關閉”文本的按鈕,您需要誘使WebDriverWait使所需元素可單擊,並且可以使用以下任一解決方案:

  • 使用CssSelector

     new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("button.close[data-dismiss='modal'][aria-label='Close']"))).Click(); 
  • 使用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