我正在使用Selenium来测试vb.net网站但是当我点击btnNoMatch时页面发生了变化,出于某种原因,看来Selenium没有更新源代码,因为我得到了以下错误。
错误
An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll
Additional information: Unable to locate element: #chkTermsAccept
我尝试过使用睡眠,刷新和导航但没有运气。如果我避免btnNoMatch单击并直接导航到页面,那么它就能够在页面上找到元素,但我无法这样做,因为在单击btnNoMatch后,信息将填充到文本框中。
守则
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()
感谢您对此问题的任何帮助。
答案 0 :(得分:1)
使用thread.sleep代替使用这样的函数来等待元素可见,你可以设置等待的时间,如果仍然没有找到它将通过超时异常错误。
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)));
}
你可以这样称呼它,它会给你时间来搜索元素。
WaitElement(driver, elementXpath).click();
WaitElement(driver, elementXpath).SendKeys(text);
或在你的情况下
Dim element10 As IWebElement = WaitElement(driver, "//*[@id= 'chkTermsAccept']");
element10.Click();