2

I am using Selenium to test a vb.net site but when I click the btnNoMatch the pages changes and for some reason, it appears that Selenium doesn't update the source code as I get the below error.

The Error

An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll

Additional information: Unable to locate element: #chkTermsAccept

I have tried using sleep, refresh and navigate but no luck. If I avoid the btnNoMatch click and navigate directly to the page then it is able to find the elements on the page but I cant do this as information is meant to be populated into text boxes after the btnNoMatch is clicked.

The Code

   driver.Navigate().GoToUrl("http://Mysite.development.ie/")

        Dim element As IWebElement = driver.FindElement(By.Id("LoginUsername"))
        element.SendKeys("UserName")

        Dim element1 As IWebElement = driver.FindElement(By.Id("LoginPassword"))
        element1.SendKeys("Password")

        Dim element2 As IWebElement = driver.FindElement(By.Id("LoginBtn"))
        element2.Click()
        System.Threading.Thread.Sleep(5000)
        Dim element3 As IWebElement = driver.FindElement(By.Id("CustomerId"))
        element3.SendKeys("1")
        'Wait Time
        System.Threading.Thread.Sleep(7000)
        Dim element4 As IWebElement = driver.FindElement(By.CssSelector(".ClickCustomer"))
        element4.Click()

        Dim element5 As IWebElement = driver.FindElement(By.Id("cbxNoMobileNo"))
        element5.Click()
        Dim element6 As IWebElement = driver.FindElement(By.Id("btnNoNumberConfirm"))
        element6.Click()
driver.FindElement(By.Id("btnNoMatch"))
            element9.Click()

        'System.Threading.Thread.Sleep(4000)
        'driver.Navigate().Refresh()
        'driver.Url = "http://Mysite.development.ie/Customer/1"
        'System.Threading.Thread.Sleep(4000)
        'driver.Manage().Window.Maximize()
        ''Dim wait As WebDriverWait = New WebDriverWait(driver, 4000)
        'wait.Until(ExpectedConditions.visibilityOfElementLocated((By.Id("id"))))

        Dim element10 As IWebElement = driver.FindElement(By.Id("chkTermsAccept"))
        element10.Click()

        Dim element12 As IWebElement = driver.FindElement(By.Name("txtName"))
        element12.SendKeys("John")

           System.Threading.Thread.Sleep(14000)
        driver.Dispose()

Thanks for any help with this issue.

2
  • put some explicit wait to find element. Commented Jul 18, 2017 at 11:20
  • Don't edit your question editing in the accepted answer... that ruins the value of the question. If people want to see the answer they can look at the accepted answer below. Commented Jul 19, 2017 at 2:08

1 Answer 1

1

In stead of using thread.sleep use a function like this to wait till the element is visible, you can set the time to wait, and if still isnt found it will through a timeout exception error.

    public IWebElement WaitElement(IWebDriver driver, String element)
    {
        IWebElement WebElement;
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); //can be changed
        return WebElement = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(element)));
    }

you can call this like, and it will give it time to search for the element.

WaitElement(driver, elementXpath).click();
WaitElement(driver, elementXpath).SendKeys(text);

or in your case

Dim element10 As IWebElement = WaitElement(driver, "//*[@id= 'chkTermsAccept']");
    element10.Click();
Sign up to request clarification or add additional context in comments.

13 Comments

I have made the changes you mentioned but it still crashes on the same line only this time i get this error due to the code updates An unhandled exception of type 'OpenQA.Selenium.WebDriverTimeoutException' occurred in WebDriver.Support.dll Additional information: Timed out after 25 seconds
Ok that means that it cannot find the elements, are you sure the path is correct?
just re-checked there tried by xpath and by the ID for some reason it works when I am on Mysite.development.ie or which ever url i navigate to originally but when it goes through the site by natural means and the url changes from something like Mysite.development.ie to Mysite.development.ie/customer/1 it can't find any elements and when I debugged I found the source code for the first page Mysite.development.ie not the current page
hmm what do you mean by natural means?
I mean the website going through the steps and btn clicks it is supposed to get to the desired destination instead of me forcing the site to navigate to the page I am having the issue on straight off the bath. If I force it to navigated it to the second url where I am having trouble first it find the elements (for example Mysite.development.ie/customer/1) it is only when the url changes that it can't find elements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.