如何在C#

时间:2019-01-19 11:33:03

标签: c# selenium selenium-webdriver xpath css-selectors

我试图在html页面中执行点击,但提示找不到该元素。

这是HTML:

<input type="button" id="btnStopSs" value="Stop" onclick="start_stop_Ss('tag_stopSs');">

代码:

IWebElement user = foxdriver.FindElement(By.Name("user_name"));
IWebElement pwd = foxdriver.FindElement(By.Name("user_passwd"));
user.Clear();
pwd.Clear();
user.SendKeys("admin");
pwd.SendKeys("admin");
IWebElement login = foxdriver.FindElement(By.Name("btnLOGIN"));
login.Click();
Thread.Sleep(3000);
IWebElement stopPage = foxdriver.FindElement(By.TagName("p"));
stopPage.Click();
Thread.Sleep(3000);
IWebElement stoptab = foxdriver.FindElement(By.Id("submenuOnLeftArea0_1"));
stoptab.Click();
Thread.Sleep(3000);
IWebElement stopbtn = foxdriver.FindElement(By.Id("'btnStopSs"));   //=>> Error OpenQA.Selenium.NoSuchElementException: 'Unable to locate element: #btnStopss'
stopbtn.Click();
Thread.Sleep(10000);
IWebElement startbtn = foxdriver.FindElement(By.Id("btnStartSs"));
stoptab.Click();

1 个答案:

答案 0 :(得分:0)

您使用的 Id 属性中似乎还有一个 ' 字符。另外,该元素似乎是动态元素,因此您需要引入 WebDriverWait ,并且可以使用以下任一解决方案:

  • Id

    new WebDriverWait(foxdriver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Id("btnStopSs"))).Click();
    
  • CssSelector

    new WebDriverWait(foxdriver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input#btnStopSs[value='Stop']"))).Click();
    
  • XPath

    new WebDriverWait(foxdriver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@id='btnStopSs' and @value='Stop']"))).Click();