Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, January 19, 2016

How to check if a web element is displayed without crashing when trying to look for non-existing element?

How to check if a web element is displayed without crashing when trying to look for non-existing element?


I want to check id certain element is displayed with selenium, buy when you try to locate certain element like:

    val editInvoiceString = driver.findElement(By.xpath( """//*[@id="edit_invoice"]/div[2]/div/div[10]/div[5]/div[1]"""))      editInvoiceString    if (editInvoiceString.isDisplayed)  do something  

if the element is not on page the program will be crashed.

its crashing already in the assignment, cause there is a findElement there and its not finding the element

How can I check if it is displayed without crash it?

Answer by alecxe for How to check if a web element is displayed without crashing when trying to look for non-existing element?


Just handle the NoSuchElementException:

try {      val editInvoiceString = driver.findElement(By.xpath( """//*[@id="edit_invoice"]/div[2]/div/div[10]/div[5]/div[1]"""));      println("element found");      // check the displayedness  }  catch {      case e: NoSuchElementException => println("no such element");  }  

Answer by Zoltn for How to check if a web element is displayed without crashing when trying to look for non-existing element?


If you use driver.find instead of driver.findElement, it will return an Option, on which you can then call isDefined to verify that the element is found.

Are you using WebBrowser?

Answer by Leon Barkan for How to check if a web element is displayed without crashing when trying to look for non-existing element?


you can achieve it by:

    public bool IsElementPresent(By by, IWebDriver driver)      {          try          {              driver.FindElement(by);              return true;          }          catch (NoSuchElementException)          {              return false;          }      }        public bool IsElementPresent(By by, IWebDriver driver, int sec)      {          bool itemExist = false;            itemExist = IsElementPresent(by, driver);          while (!itemExist && sec >= 0)          {              thread.Sleep(1);              itemExist = IsElementPresent(by, driver);              sec--;          }            if (sec == -1)              return false;          else              return true;      }  

Call:

if(IsElementPresent(By.xpath("""//*[@id="edit_invoice"]/div[2]/div/div[10]/div[5]/div[1]"""),driver,5))  {  //case exist  }  else  {  // case not exist  }  

Answer by djangofan for How to check if a web element is displayed without crashing when trying to look for non-existing element?


You don't need to use a try-catch clause if you ignore the exception using the Wait class (which extends FluentWait):

Wait wait = new FluentWait(driver)     .withTimeout(30, SECONDS)     .pollingEvery(5, SECONDS)     .ignoring(NoSuchElementException.class);  WebElement foo = wait.until(ExpectedConditions.presenceOfElementLocated(    By.xpath(".//*[@id="edit_invoice"]/div[2]/div/div[10]/div[5]/div[1]"))  );  

Answer by Ardesco for How to check if a web element is displayed without crashing when trying to look for non-existing element?


Use findElements instead of FindElement.

This will return a list of WebElements found. If no elements are found the list will be empty. It would look something like this (Note I'm not a Scala person so the syntax below may be slightly incorrect).

val editInvoiceString = driver.findElements(By.xpath( """//*[@id="edit_invoice"]/div[2]/div/div[10]/div[5]/div[1]"""))    if (editInvoiceString size > 0)  do something  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.