【发布时间】:2020-03-24 17:15:33
【问题描述】:
我正在运行 selenium,使用以下 nuGet 包(DotNetSeleniumExtras.WaitHelpers.3.11.0 NUnit.3.12.0 NUnit3TestAdapter.3.16.1 Selenium.Support.3.141.0 Selenium.WebDriver.3.141.0)。我正在使用 Chrome 的 Windows 上运行(版本 80.0.3987.132(官方构建)(64 位))
迄今为止,除了一些时间问题外,一切都正常。 但是,如下所述的按钮拒绝接受任何点击。
<mat-card-actions _ngcontent-frq-c15="" class="mat-card-actions" style="margin-top: 3rem">
<button _ngcontent-frq-c15="" color="primary" mat-raised-button="" type="submit" class="mat-raised-button mat-button-base mat-primary" ng-reflect-color="primary"><span class="mat-button-wrapper">Submit</span>
<div class="mat-button-ripple mat-ripple" matripple="" ng-reflect-centered="false" ng-reflect-disabled="false" ng-reflect-trigger="[object HTMLButtonElement]"></div>
<div class="mat-button-focus-overlay"></div>
</button>
<button _ngcontent-frq-c15="" mat-stroked-button="" type="reset" class="mat-stroked-button mat-button-base"><span class="mat-button-wrapper">Clear</span>
<div class="mat-button-ripple mat-ripple" matripple="" ng-reflect-centered="false" ng-reflect-disabled="false" ng-reflect-trigger="[object HTMLButtonElement]"></div>
<div class="mat-button-focus-overlay"></div>
</button>
我从代码角度尝试了所有方法,见下文
string[] selectors =
{
".mat-card-actions .mat-raised-button", ".mat-card-actions .mat-primary",
".mat-card-actions .mat-button-wrapper", ".mat-card-actions .mat-button-ripple"
};
//
// string[] selectors = {".mat-card-actions .mat-raised-button"};
foreach (string selector in selectors)
{
IWebElement submitButton = _driver.FindElement(By.CssSelector(selector));
submitButton.Click();
try
{
submitButton.SendKeys(Keys.Space);
submitButton.SendKeys(Keys.Return);
}
catch (Exception ex)
{
log.Error(ex.Message);
}
// This doesn't work
// Configure the Action
Actions action = new Actions(_driver);
action.MoveToElement(submitButton).Perform();
action.MoveToElement(submitButton).Click().Perform();
// This doesn't work
var executor = (IJavaScriptExecutor) _driver;
executor.ExecuteScript("arguments[0].click();", submitButton);
executor.ExecuteScript("document.querySelector(\".mat-raised-button\").click()");
}
这些都不起作用。然后我注意到在 chrome 控制台窗口中执行以下 javascript 不起作用。
document.querySelector(\".mat-raised-button\").click()"
但是,这在不是由 Selenium 驱动的 Chrome 实例中运行良好。这是 Chrome / Selenium 还是 C# 问题?所有其他按钮单击在被测应用程序中都可以正常工作。 我已经在这个问题上失去了 2 天的时间,所以任何帮助都将不胜感激。
谢谢,
约翰
【问题讨论】:
-
document.querySelector(".mat-raised-button")指向正确的元素,您在硒触发的浏览器中运行它?当事情没有奏效时你会得到吗? -
是的,上面的例子已经被削减了。关键是在 chrome 中运行时,javascript .click 有效。在 ChromeDriver (chrome) 中运行时,.click 不起作用。
标签: angular selenium google-chrome button click