How to identify the web-element for a Text Box using Selenium



  • I am attempting to locate the empty textbox by the input id 'Department_input'

    <input id="Department_input" type="text" aria-required="true" size="40" class="field_input ui-widget-content" maxlength="15" aria-invalid="false">
    

    Using driver.find_element_by_xpath("//input[@id='Department_input']") without luck(returning no such element error). There are multiple text boxes on the form that I'd like to enter into also.

    Is there another way to approach this?



  • Most probably this occurs because of the execution speed. The code executes before loading the page. Therefore when the code looks for an element with the id='Department_input' it cant find such element and throws exception.

    First of all try a Thread.sleep(5000) just before where you look for the element.

    Thread.sleep(5000);    
    driver.find_element_by_xpath("//input[@id='Department_input']");     
    

    This will pause the execution for 5 seconds and continue. If it works for you try using page factory.
    It's better if you use like this since you have ID of the element,

    WebElement element = driver.findElement(By.id("Department_input"));     
    

    page-object-pattern-model-page-factory 1

    page-object-pattern-model-page-factory 2

    Check these 2 tutorials. It will help to understand. Both are for the same task.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2