Unable to enter text to input type on Webpage using Selenium Java

前端 未结 3 1616
无人及你
无人及你 2021-01-28 16:54

Uploaded my entire webpage on which my test is failing here: https://drive.google.com/file/d/1WHcwpQFi5Cxh1q1MupQEuSPk6CPZs2GC/view?usp=sharing

Below is my Selenium Java

相关标签:
3条回答
  • 2021-01-28 17:28

    I think the element which you needed is present on the DOM, but it is not in a state that you can interact with them.

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(By.id("crtestrequest-cr_app_name")));
    WebLement input = driver.findElement(By.id("crtestrequest-cr_app_name"))
    input.click();
    input.clear();
    input.sendKeys("123");
    

    or

    for(int i=0; i<=2;i++){
      try{
         WebLement input = driver.findElement(By.id("crtestrequest-cr_app_name"))
         Actions actions = new Actions(driver);
         actions.moveToElement(input).perform();
         input.click();
         break;
       } catch (StaleElementReferenceException e) {
               System.out.println("Trying to recover from a stale element :" + e.getMessage());
       }
    }
    
    0 讨论(0)
  • 2021-01-28 17:29

    Instead of invoking sendKeys() to the <label> you need to invoke on the <input> element and ideally to click() on the element you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

    • cssSelector:

      WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.row div.field-crtestrequest-cr_app_name>label[for='crtestrequest-cr_app_name']")));
      element.click();
      element.clear();
      element.sendKeys("Allocation");
      
    • xpath:

      WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='row']//div[contains(@class, 'field-crtestrequest-cr_app_name')]/label[@for='crtestrequest-cr_app_name']")));
      element.click();
      element.clear();
      element.sendKeys("Allocation");
      

    References

    You can find a couple of relevant discussions on ElementNotInteractableException in:

    • Selenium WebDriver throws Exception in thread “main” org.openqa.selenium.ElementNotInteractableException
    • org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard: while sending text to FirstName field in Facebook
    0 讨论(0)
  • 2021-01-28 17:38

    I have been trying past 24 hours to get this to work and finally did.

    Before, I trigger the Search Assests button as visible in my video @ 1.11 minutes I tried the below code which worked.

    WebElement ele = driver.findElement(By.id("crtestrequest-cr_app_code"));
    JavascriptExecutor exec = (JavascriptExecutor)driver;
    exec.executeScript("arguments[0].click();", ele);
    driver.findElement(By.id("crtestrequest-cr_app_code")).clear();
    driver.findElement(By.id("crtestrequest-cr_app_code")).sendKeys("Allocation");
    
    0 讨论(0)
提交回复
热议问题