未找到元素 - 硒

时间:2015-04-02 05:06:19

标签: selenium selenium-webdriver

我已经写了一段代码来登录一个工作正常的应用程序。现在我必须单击一个添加按钮,我已经通过Id XPathClassName尝试了它,但它只是给了我未找到元素的例外。我以为我应该应用一个明确的等待,但它也没有用。请检查下面的代码:

public static void Login()

{
    Browser.Url = "http://example.com";
    _username = Browser.FindElement(By.Id("UserName"));
    var password = Browser.FindElement(By.Id("Password"));
    var loginbtn = Browser.FindElement(By.ClassName("btn-primary"));

    _username.SendKeys("admin");
    password.SendKeys("123");
    loginbtn.Click();

    var supplierTab = Browser.FindElement(By.Id("mainSupplier"));
    supplierTab.Click();

    WebDriverWait wait = new WebDriverWait(Browser, TimeSpan.FromSeconds(20));
    IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
    {
       try
        {
            return d.FindElement(By.Id("btnAddSupplier_SupplierForm"));
        }
        catch
        {
            return null;
        }
    });

    var addbtn = Browser.FindElement(By.Id("btnAddSupplier_SupplierForm"));
    addbtn.Click();

}

这总是在第二行代码中找不到元素的异常。

这是HTML:

enter image description here

4 个答案:

答案 0 :(得分:0)

尝试以下

public static void Login()

{
    Browser.Url = "http://example.com";
    _username = Browser.FindElement(By.Id("UserName"));
    var password = Browser.FindElement(By.Id("Password"));
    var loginbtn = Browser.FindElement(By.ClassName("btn-primary"));

    _username.SendKeys("admin");
    password.SendKeys("123");
    loginbtn.Click();

    //I think you have mentioned the iframe exist and assuming the element is inside the iframe do the following. If not skip the SwitchTo() part


    //you can use name, css to identify the iframe
    Browser.SwitchTo().Frame(Browser.FindElement(By.XPath("xpath for the iframe")));

    var supplierTab = Browser.FindElement(By.Id("mainSupplier"));
    supplierTab.Click();

    WebDriverWait wait = new WebDriverWait(Browser, TimeSpan.FromSeconds(20));
    IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
    {
        return d.FindElement(By.Id("btnAddSupplier_SupplierForm"));
    });

    //if you think the id is not unique try using xpath or css
    //even though you added an explicit wait you never used it
    myDynamicElement.Click();

}

答案 1 :(得分:0)

有时元素将存在于源代码中,但对于selenium执行单击操作将不可见。尝试下面的代码,它将一直等到元素可见:

WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(20));
IWebElement element = wait.Until(
ExpectedConditions.ElementIsVisible(By.Id("btnAddSupplier_SupplierForm")));
element.Click();

答案 2 :(得分:0)

不确定这是否有帮助,但在点击“添加”按钮之前尝试调用此函数:

void waitForPageLoad(WebDriver driver)
    {
        ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
        };  
        wait.until(pageLoadCondition);
    }

答案 3 :(得分:0)

当使用的定位机制在dom中不可用时,抛出NoSuchElementException。

如果您的预期条件在时间限制内未成立,则抛出TimeoutException。预期的条件可以是任何东西,(存在,可见性,属性值等......)。

首先,您要弄清楚您使用的元素的定位机制是否确实在dom中找不到。这是一个手动查找的好方法。

  1. 打开Chrome并导航到该页面。
  2. 打开chrome dev工具,然后单击控制台选项卡。
  3. 在文本框中输入$ x(“// * [@ id ='btnAddSupplier_SupplierForm']”)并按Enter键。这将运行xpath查询,基本上您正在查找具有值为“btnAddSupplier_SupplierForm”的id属性的任何元素。
  4. 如果元素出现在控制台中并且是正确的元素,则可能是您在dom完成加载之前尝试在dom中找到元素。如果控制台中没有出现任何元素,那么您的定位器就会很糟糕。
  5. 请报告您的发现。

相关问题