C# Selenium 找不到按钮

C# Selenium can't find button

<form name="loginForm" ng-submit="login()" autocomplete="off" class="ng-pristine ng-invalid ng-invalid-required">
<button type="submit" class="icon-login" ng-disabled="!loginForm.$valid" disabled="enabled"></button>
<input type="text" name="username" id="username" placeholder="Username / Email" autocapitalize="off" autocorrect="off" required="" ng-model="credentials.username" class="ng-pristine ng-invalid ng-invalid-required">
<input type="password" name="password" id="password" placeholder="Password" autocapitalize="off" autocorrect="off" required="" ng-model="credentials.password" class="ng-pristine ng-invalid ng-invalid-required">
</form>    

我试过了:

  IWebElement button = driver.FindElement(By.ClassName("icon-login"));

但这没有用,当我截取屏幕截图时,该按钮似乎不可见,而它应该是...

我也试过使用 wait until 但即使等待一分钟也会超时

编辑:

IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(3)).Until(ExpectedConditions.ElementExists(By.XPath("//form[@name='loginForm']/button[@class='icon-login']")));

试过了,但它只是超时,元素确实存在但不可点击

根据您的评论更新以及 url用户名密码 字段和 Login 按钮都在一个表单中。因此,一旦您填写了 UsernamePassword 字段,您就可以直接调用 Submit() 方法登录网站,如下所示:

new WebDriverWait(driver, TimeSpan.FromSeconds(3)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@id='username' and @name='username']"))).SendKeys("refactorcoding");
driver.FindElement(By.XPath("//input[@id='password' and @name='password']")).SendKeys("refactorcoding");
driver.FindElement(By.XPath("//input[@id='password' and @name='password']")).Submit();