代码之家  ›  专栏  ›  技术社区  ›  King81

Selenium WebDriver-无法在模式对话框中找到并单击提交按钮

  •  1
  • King81  · 技术社区  · 7 年前

    我还实现了一个隐式等待按钮,它似乎不起作用:

    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
    

    结果堆栈跟踪:

    at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
       at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
       at `OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)`
       at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath)
       at OpenQA.Selenium.By.<>c__DisplayClass19_0.<XPath>b__0(ISearchContext context)
       at OpenQA.Selenium.By.FindElement(ISearchContext context)
       at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
    

    测试方法测试用例。InsTerminationCollab引发异常:

    OpenQA.Selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='create - experiment - confirm - title']"}
    (Session info: chrome=64.0.3282.167)
    (Driver info: chromedriver=2.35.528161 
    (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.14393 x86_64)
    

    HTML代码:

    <div class="modal-content" role="document"><div class="modal-header"><button type="button" class="close"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button><h4 id="create-experiment-confirm-title" class="modal-title">Do you wish to proceed with the following selections?</h4></div><form class="form-horizontal" method="post"><div class="modal-body">
    
    <button type="submit" title="Create experiment" id="create-experiment-confirm-submit" class="btn btn-primary">Submit</button>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   undetected Selenium    7 年前

    定位 点击 这个 提交 模式对话框上的按钮您可以使用以下代码行:

    wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
    element = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//form[@class='form-horizontal']//button[@class='btn btn-primary' and @id='create-experiment-confirm-submit']")));
    element.Click();