Как найти кнопку с селеном WebDriver в C # - PullRequest
0 голосов
/ 19 января 2019

Я пытался выполнить щелчок на моей 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 голосов
/ 19 января 2019

Кажется, есть дополнительный ' символ в атрибуте 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();
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...