以下是我的代码:
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using NUnit.Framework;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
namespace Test01
{
class Program
{
static void Main(string[] args)
{
IWebDriver webDriver = new EdgeDriver(@"C:\Users\renuka.rani\Desktop\Selenium projects\Test01");
webDriver.Navigate().GoToUrl("http://d2ubgnp33uouus.cloudfront.net/");
webDriver.Manage().Window.Maximize();
webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
webDriver.FindElement(By.XPath(".//*[@id='main']/header/div/div/div/div[1]/ul/li[1]/a")).Click();
webDriver.FindElement(By.Name("email")).SendKeys("renuka.rani@tradingpost.com");
webDriver.FindElement(By.Name("password")).SendKeys("renukarani");
webDriver.FindElement(By.Name("btnSignInTest")).Click();
webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
string actualText = webDriver.FindElement(By.LinkText("Renuka")).GetAttribute("Renuka");
Assert.AreEqual(actualText, "Renuka");
Actions builder = new Actions(webDriver);
builder.MoveToElement(webDriver.FindElement(By.LinkText("MyTradingpost"))).Click().Build().Perform();
webDriver.Quit();
}
}
}`
到达此行时
webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
webDriver.FindElement(By.XPath(".//*[@id='main']/header/div/div/div/div[1]/ul/li[1]/a")).Click();
它抛出异常
webdriver.dll中出现System.InvalidOperationException“。无法点击超链接”登录“。
答案 0 :(得分:1)
有时在启动期间会有延迟,并且应通过诱导延迟来处理。 你的模拟代码与我合作。
static void Main(string[] args)
{
IWebDriver driver;
driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://d2ubgnp33uouus.cloudfront.net/");
driver.Manage().Window.Maximize();
Thread.Sleep(2000);
//driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
driver.FindElement(By.XPath(".//*[@id='main']/header/div/div/div/div[1]/ul/li[1]/a")).Click();
Thread.Sleep(3000);
driver.FindElement(By.Name("email")).SendKeys("renuka.rani@tradingpost.com");
driver.FindElement(By.Name("password")).SendKeys("renukarani");
driver.FindElement(By.Name("btnSignInTest")).Click();
Thread.Sleep(3000);
//driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
// string actualText = driver.FindElement(By.LinkText("Renuka")).GetAttribute("Renuka");
IWebElement user = driver.FindElement(By.XPath("//*[@id='main']/header/div/div/div/div[1]/ul/li[1]/a/strong"));
String actualText = user.Text;
if (actualText.Equals("Renuka"))
{
Actions builder = new Actions(driver);
Thread.Sleep(2000);
builder.MoveToElement(driver.FindElement(By.XPath("//*[@id='main']/header/div/div/div/div[1]/ul/li[2]/ul/li[7]/a"))).Click().Build().Perform();
driver.Quit();
}
}
}
}