【问题标题】:How to click on the button with text as Close within a modal dialog box as per the html through Selenium and C#如何通过 Selenium 和 C# 根据 html 在模态对话框中单击文本为关闭的按钮
【发布时间】:2019-01-31 07:42:03
【问题描述】:

我使用 C# 使用 Selenium Webdriver,但 selenium 无法识别我的模态窗口。

我试过了:

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

但它不起作用。在代码中,模态框具有“ID”,但它也不起作用。

我需要识别模态并单击按钮。

【问题讨论】:

  • 嗨。如果您将代码作为文本包含在您的问题中,用户可以复制/粘贴它以进行尝试,从而更容易为您提供帮助。
  • 您确定模态框对您可见吗?尝试切换到它?您是否尝试过先检查元素是否可见?
  • 感谢您的提示:D 真的,我的模式不可见。

标签: c# selenium xpath css-selectors webdriver


【解决方案1】:

根据您共享的 HTML,单击带有文本为 Close 的按钮,您需要为所需的 诱导 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();
    

【讨论】:

    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2022-07-25
    • 2018-12-17
    • 2019-01-20
    • 1970-01-01
    • 2021-12-31
    • 2023-03-10
    相关资源
    最近更新 更多