-1

I tried these(in scenario when the element does not exist) and always getting

OpenQA.Selenium.NoSuchElementException


If driver.FindElement(By.XPath("//input[@id='username']")).Displayed = True Then
     driver.FindElement(By.XPath("//input[@id='username']")).Click()
 End If

  If driver.FindElement(By.XPath("//input[@id='username']")) = True Then
        driver.FindElement(By.XPath("//input[@id='username']")).Click()

    If driver.FindElement(By.XPath("//input[@id='username']")).Displayed Then
        driver.FindElement(By.XPath("//input[@id='username']")).Click()
Fabulous
  • 2,393
  • 2
  • 20
  • 27
Alex
  • 1
  • 2

2 Answers2

0

You need to use try catch block to handle this & then execute the action on the element if it isn't null.Something like this :

webelement ele = null
try
{
   ele = driver.FindElement("your condition");
}
catch(NoSuchElementException ex)
{
  #do-nothing
}
if(ele != null)
{
   #perform your action
}
SnR
  • 319
  • 1
  • 7
0

Assuming that the WebDriver API is the same for VB as it is for Java, you can use the same approach as suggested here: https://stackoverflow.com/a/9188374/5803406.

devNull
  • 3,849
  • 1
  • 16
  • 16