如何使用selenium和c#传递页面

时间:2017-06-23 13:30:11

标签: c# web-services selenium boolean

这很可能是我的代码中的逻辑错误。 首先,我要做的是:

  • 转到我网站的相应页面,在这种情况下link并使用我的公开无效GDataPicker收集数据。

  • 现在我希望你帮助我,我使用下面的代码来查看网页中是否存在下一个按钮,并收集它的相应数据,但总是给我同样的错误:

  

OpenQA.Selenium.StaleElementReferenceException:'陈旧元素引用:元素未附加到页面文档     (会议信息:chrome = 58.0.3029.110)     (驱动程序信息:chromedriver = 2.30.477700(0057494ad8732195794a7b32078424f92a5fce41),platform = Windows NT 10.0.15063 x86_64)',我想这可能是因为我没有更新我的NextButtonElement。

代码:

Boolean ElementDisplayed;
try
{
    Gdriver.Navigate().GoToUrl("http://www.codigo-postal.pt/");
    IWebElement searchInput1 = Gdriver.FindElement(By.Id("cp4"));
    searchInput1.SendKeys("4710");//4730
    IWebElement searchInput2 = Gdriver.FindElement(By.ClassName("cp3"));
    searchInput2.SendKeys("");//324
    searchInput2.SendKeys(OpenQA.Selenium.Keys.Enter);
    IWebElement NextButtonElement = Gdriver.FindElement(By.XPath("/html/body/div[4]/div/div/div[2]/ul/li[13]/a"));
    GDataPicker();

    while (ElementDisplayed =  NextButtonElement.Displayed)
    {
        GDataPicker();
        Gdriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2000));
        NextButtonElement.SendKeys(OpenQA.Selenium.Keys.Enter);
    }
}
catch (NoSuchElementException i)
{
    ElementDisplayed = false;
    GDataPicker();
}

2 个答案:

答案 0 :(得分:0)

我无法帮助您使用C#,但是当您操作的元素仍在dom中但已被替换为相同的元素时,会发生StaleElementReferenceException。我会做的是捕获该异常并再次找到该元素

catch (StaleElementReferenceException i)
    {

        IWebElement NextButtonElement = Gdriver.FindElement(By.XPath("/html/body/div[4]/div/div/div[2]/ul/li[13]/a"));

    }

http://www.seleniumhq.org/exceptions/stale_element_reference.jsp

答案 1 :(得分:0)

我会将ExpectedConditions.ElementToBeClickable与动态等待功能selenium一起使用。

var wait = new WebDriverWait(GDriver, TimeSpan.FromSeconds(5));
IWebElement NextButtonElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("/html/body/div[4]/div/div/div[2]/ul/li[13]/a")));

ExpectedConditions.ElementToBeClickable完全按照您的意愿执行,稍等一会儿,直到元素显示而不是陈旧。

        /// <summary>
    /// An expectation for checking an element is visible and enabled such that you
    /// can click it.
    /// </summary>
    /// <param name="locator">The locator used to find the element.</param>
    /// <returns>The <see cref="IWebElement"/> once it is located and clickable (visible and enabled).</returns>
    public static Func<IWebDriver, IWebElement> ElementToBeClickable(By locator)
    {
        return (driver) =>
        {
            var element = ElementIfVisible(driver.FindElement(locator));
            try
            {
                if (element != null && element.Enabled)
                {
                    return element;
                }
                else
                {
                    return null;
                }
            }
            catch (StaleElementReferenceException)
            {
                return null;
            }
        };
    }

来自https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/support/UI/ExpectedConditions.cs