c# - 如何关闭遮挡其他元素的永久元素?

标签 c# css selenium element sendkeys

我无法点击某个元素,因为下拉菜单遮住了所有其他元素。

在 Visual Studio 中使用 Selenium,我试图构建一个测试用例,我首先单击下拉菜单中的复选框,然后单击下拉菜单外的另一个元素。但是,在您单击第一个复选框后,下拉菜单不会自行关闭。

如果您在网络浏览器上手动关闭此下拉菜单,您只需按 Esc 键或单击下拉菜单外的某处即可。但是,当我尝试将其自动化时,它不起作用。

我试过在脚本中像这样按下 Esc 键:

Actions action = new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Escape);

但它不起作用。它不会发送有关发送 Esc 键的错误,而是在尝试单击被遮挡的元素时在下一行发送超时:

OpenQA.Selenium.ElementClickInterceptedException : Element <div class="mat-radio-outer-circle"> is not clickable at point (116,608) because another element <div class="cdk-overlay-backdrop cdk-overlay-transparent-backdrop cdk-overlay-backdrop-showing"> obscures it

我也试过不发送 Esc 键,而是像这样在下拉菜单外单击:

wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//div[3]/div[3]"))).Click();

这在 Visual Studio 中不起作用,但它在 Selenium IDE 中确实有效,只需使用命令单击并将 //div[3]/div[3] 设置为目标即可。

Selenium IDE Example

我曾尝试使用 IDE 中的选择功能来识别未包含在下拉菜单中的其他元素。我也试过使用 Firebug 。但这是唯一可在下拉菜单之外单击的元素。

Firebug view

总结:

  1. 如果我发送“Esc”的代码不正确,请告诉我。

  2. 为什么 Visual Studio 无法识别并单击 //div[3]/div[3] 即在下拉菜单之外,而在 Selenium 中可以执行此操作集成开发环境?

  3. 还有其他方法可以关闭下拉菜单吗?

  4. 我了解到您始终可以单击使用 javascript 隐藏的元素,但我没有找到如何在 C# 中执行此操作的指南。如果你们知道该怎么做,请告诉我。

最佳答案

我总是尝试通过直接使用列表项的 xpath 或 css 来选择列表项,而不是单击列表框并进行选择。这样我们就可以忽略这个麻烦,而且速度也更快。 怎么做:

driver.FindElement(By.Xpath("//select[@attribute='attributeValue']/li[@attribute='attributeValue'])).click

这是 Javascript 方法。引用以下 2 个链接 https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html

https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_IJavaScriptExecutor_ExecuteScript.htm

IWebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
// if you want to click on element
IWebElement element = driver.FindElement(By.CssSelector("your css locator goes here")) 

// if you want to return value from javascript and store
string title = (string)js.ExecuteScript("return document.title");

如果要隐藏遮挡元素,这里是使用js的逻辑。

element = driver.FindElement(By.Xpath("//div[@class='cdk-overlay-backdrop cdk-overlay-transparent-backdrop cdk-overlay-backdrop-showing']")); // this is the obscuring element in your case.
js.ExecuteScript("arguments[0].setAttribute("style","display: none;");",element);

关于c# - 如何关闭遮挡其他元素的永久元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55516140/

相关文章:

selenium - ExpectedConditions.invisibilityOfElementLocated 不起作用

c# - .NET Core 2.0 MVC - 在调用 TryUpdateModelAsync 之前以编程方式更改值

css - SCSS/CSS 选择器选择所有输入类型

html - 我正在大修我的博客,并且……HTML5 可以吗?

python - 如何解决 VS 代码中没有名为 'selenium' 的模块?

javascript - 从下拉菜单中逐页阅读 - 在第二页找不到下拉菜单

javascript - 如何为 csv 字符串的精确连续元素设置顺序?

c# - 不支持带有 Autofac 的 Net Core 3.0 抛出 IServiceProvider

c# - 在 Xamarin.Forms 页面中隐藏 iPhone X 的主页指示器

javascript - 修复列宽未知时的溢出问题