Megan Smoak
Megan Smoak

Reputation: 1

OpenQA.Selenium.NoSuchElementException

class Program
{
    static void Main(string[] args)
    {
        IWebDriver driver = new ChromeDriver();

        //Dev environment
        driver.Url = /*URL*/;

        var trialGroupName = driver.FindElement(By.Name("TrialGroupName")); 
        trialGroupName.SendKeys(/*trial group name */);

        var userName = driver.FindElement(By.Name("UserName"));
        userName.SendKeys(/*username*/);

        var password = driver.FindElement(By.Name("UserPassword"));
        password.SendKeys(/*password*/);

        var loginButton = driver.FindElement(By.Id("Ok"));
        loginButton.Click();

        //Find and click on Browse tab
        var browseTab = driver.FindElement(By.Id("17"));
        browseTab.Click();
    }
}

OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"id","selector":"17"} (Session info: chrome=58.0.3029.110) (Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 6.1.7601 SP1 x86_64)'

Here's the inspect information:

<td>
<a id="17" class="defaultMenu initMenu" href="javascript:onMenuHref(17)" notran="">Browse</a>
</td>

I've also tried running it with the XPath (both the short version from Chrome and the long version from FireBug) and I receive the same error. I've tried adding in an implicit wait and a Thread.Sleep() but still received the same error. Also tried starting on a different field and tabbing to it and then it gave me the same exception but on the new field. I also tried playing around with cssSelector this morning and couldn't get that to work either. I did try to do an explicit wait but I'll be honest, I'm still trying to digest and understand it as waits are a new concept and I don't fully understand it, so I don't think I'm doing it right.

I'm a beginner, and I'm currently just trying to write the script to get it to the actual page that needs testing. I've been teaching myself some coding and selenium as I'm the only QA person at my company right now and I really think we could benefit from automation. This portion of the page isn't actually programmed by us it's done by a third party who hosts the system we use so I'm limited to what I see in the inspect.

(I commented out the user login information and URL because it is for internal use only and the page can't be reached outside of our systems anyways. Also I'm pretty sure I'm not allowed to do so by my employer).

Any help on this would be greatly appreciated, or any additional resources I can use. I've been using google mostly so there may be other pages you are aware of that could help me that I haven't found yet.

Upvotes: 0

Views: 3250

Answers (1)

Timothy Cope
Timothy Cope

Reputation: 482

The login button takes you to a new page, which you need to wait for it to load. Try using an explicit wait to wait for the tab to be clickable. The timeout is 10 seconds in the following example, so modify that as needed.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement browseTab = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("17")));

Upvotes: 3

Related Questions