C# WebDriver:单击到元素时,显式等待不起作用
我的隐式等待代码运行良好。但是我阅读了关于等待的信息,并且理解,我需要在我的项目中显式地使用等待。这就是为什么我要用它来实现我的测试项目。 当我的alhorithm Equal的步骤单击按钮时,我有错误: 请帮帮我 基类:C# WebDriver:单击到元素时,显式等待不起作用,c#,selenium,webdriver,nunit,C#,Selenium,Webdriver,Nunit,我的隐式等待代码运行良好。但是我阅读了关于等待的信息,并且理解,我需要在我的项目中显式地使用等待。这就是为什么我要用它来实现我的测试项目。 当我的alhorithm Equal的步骤单击按钮时,我有错误: 请帮帮我 基类: using NUnit.Framework; using System; using LinkedinAddContacts.Pages; using LinkedinAddContacts.TestData; using OpenQA.Selenium; using Ope
using NUnit.Framework;
using System;
using LinkedinAddContacts.Pages;
using LinkedinAddContacts.TestData;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace LinkedinAddContacts
{
[TestFixture]
public class TestClass
{
private IWebDriver webDriver;
private WebDriverWait waitDriver;
[SetUp]
public void InitializeBrowser()
{
webDriver = new ChromeDriver();
waitDriver = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
webDriver.Manage().Window.Maximize();
webDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);
webDriver.Navigate().GoToUrl("https://www.linkedin.com/");
}
[Test]
public void TestMethod()
{
Authorization authorizationData = new Authorization();
StartPage objStartPage = new StartPage(waitDriver);
NetworkPage objNetworkPage = new NetworkPage(waitDriver);
objStartPage.EntrySystem(authorizationData);
objNetworkPage.ConnectPeople();
}
[TearDown]
public void CloseBrowser()
{
webDriver.Quit();
}
}
}
中学班级:
using NUnit.Framework;
using LinkedinAddContacts.TestData;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace LinkedinAddContacts.Pages
{
public class StartPage
{
// private IWebDriver webDriver;
private WebDriverWait waitDriver;
#region Objects
public StartPage(WebDriverWait waitDriver)
{
this.waitDriver = waitDriver;
}
private IWebElement EmailInput
{
get
{
return waitDriver.Until(ExpectedConditions.ElementToBeClickable(By.Name("session_key")));
//return webDriver.FindElement(By.Name("session_key"));
}
}
private IWebElement PasswordInput
{
get
{
return waitDriver.Until(ExpectedConditions.ElementToBeClickable(By.Name("session_password")));
// return webDriver.FindElement(By.Name("session_password"));
}
}
private IWebElement LoginButton
{
get
{
return waitDriver.Until(ExpectedConditions.ElementToBeClickable(By.Name("login-submit")));
//return webDriver.FindElement(By.Id("login-submit"));
}
}
private IWebElement RegistrationForm
{
get
{
return waitDriver.Until(ExpectedConditions.ElementToBeClickable(By.Id("regForm")));
// return webDriver.FindElement(By.Id("regForm"));
}
}
#endregion
#region Methods
public void CloseRegistrationForm()
{
IJavaScriptExecutor js = waitDriver as IJavaScriptExecutor;
js.ExecuteScript("document.getElementById('regForm').style.display = 'none';");
// ((IJavascriptExecutor)driver).executeScript("scroll(0,400)");
}
public void EntrySystem(Authorization authorizationData)
{
// CloseRegistrationForm();
EmailInput.SendKeys(authorizationData.Email);
PasswordInput.SendKeys(authorizationData.Password);
LoginButton.Click();
}
#endregion
}
}
出现错误:
public void EntrySystem(Authorization authorizationData)
{
// CloseRegistrationForm();
EmailInput.SendKeys(authorizationData.Email);
PasswordInput.SendKeys(authorizationData.Password);
LoginButton.Click();
}
当我正确理解时,您的代码会在以下行崩溃:
return waitDriver.Until(ExpectedConditions.ElementToBeClickable(By.Name("login-submit")));
现在,看看linkedIn的起始页面,就会发现login submit按钮没有定义name属性,但是你可以使用它的id
<input tabindex="1" id="login-submit" class="login submit-button" type="submit" value="Einloggen">
因此,您应该使用
By.id()
而不是By.name()
注意您使用的web驱动程序很重要
首先,正如@Robert所说的,只要你能找到,最好通过Id找到
其次,我认为LoginButton.Click()不起作用。我对chrome驱动程序有这样的问题。更改页面比例(缩放)时,单击方法无法正常工作,或者单击页面上的其他位置
我建议您对任何单击操作使用SendKeys。
就这样,
LoginButton.SendKeys(Keys.Enter);// or Keys.Return
永远不要使用点击法谢谢你的建议,我相信这是我的技能。我哪儿也没说.你的建议在另一个项目上对我有帮助。我遭受了一个多星期的按钮不可点击。再次感谢