0
<input type="submit" value="Add New Date of Commencement of Contract Details for INDoS No. : 09HL9630" onclick="method.value='loadFrom3A'" xpath="1">

This is what in inspect element and this is what I tried but it's not working. Xpath is not working as it is changing in loop form. Please help

public void addnew_date() {

    driver.findElement(By.cssSelector("input[type='submit']").click();
    driver.manage().timeouts().pageLoadTimeout(40,TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Preeti
  • 7
  • 7

3 Answers3

0

Please use this below xpaths.

Xpath's: 1. //input[@type='submit']
2. //input[contains(text(),'Add New Date of Commencement')]

public void addnew_date() {
    driver.findElement(By.xpath("//input[@type='submit']").click();
}

Implicitwait and Pageloadtimeout should be used at the start of the test, just after the Creation of webdriver object i.e. driver.

something like:

System.setProperty("webdriver.chrome.driver",".\\drivers\\chromedriver.exe");
    WebDriver   driver = new ChromeDriver(options);
    driver.get("https://www.google.com");
    driver.manage().timeouts().pageLoadTimeout(40,TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
Rakesh
  • 118
  • 1
  • 9
  • Your XPath #1 is exactly the same as what OP is already using... how does that potentially solve the problem? – JeffC Mar 13 '19 at 18:38
  • 1
    Jeffc : please try with the xpaths given by me and the one which op has already written in question, see which one actually works and then speak. – Rakesh Mar 14 '19 at 01:23
  • That doesn't answer my question... you switched the exact same locator from a CSS selector to an XPath... that won't magically start working. – JeffC Mar 14 '19 at 01:56
  • 1
    Mr jeffc Try both and check which one works and see the magic happening.. thats how it solves – Rakesh Mar 14 '19 at 02:02
  • No, there's no magic here. The first one will not work because it's the same as what OP posted. – JeffC Mar 14 '19 at 03:03
0

You need to induce WebDriverWait in conjunction with ExpectedConditions as elementToBeClickable() and you can use the following Locator Strategy:

  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[starts-with(@value, 'Add New Date of Commencement of Contract Details') and contains(@onclick, 'loadFrom3A')]"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

There can be the chances that the intended element is present but not clickable or display so it throws the said exception. Need to identify the actual cause. try the following way to debug :

  • make sure you have the single element with the locator input[type='submit'] there can be some other input tags with submit type and that can be the first and not intractable. So need to make it more specific by adding some surrounding elements or attributes e.g. //input[type='submit'][contains(@value,'Add New Date')]

  • perform the check for current state of element e.g.

    List<WebElement> button = driver.findElements(By.cssSelector("input[type='submit']");
    
    System.out.println(button.size()) // 1 means element present 0 means no element and other count mean number of element
    
    if(button.size()==1){ //  check element is present
         if(button.get(0).isDisplayed()){ // check element is visible
            System.out.println("element is present and displaying");
            button.get(0).submit();
         }else{
            System.out.println("element is present but not displaying");
          }
    else if (button.size()>1){
        System.out.println("there are multiple element with same locator");
    
    } else {
        System.out.println("element not present for the locator");
    }
    
  • If element require some time to get interact then use selenium wait conditions wisely

    1. ImplicitWait example :

      System.setProperty("webdriver.chrome.driver",".\\drivers\\chromedriver.exe");
      WebDriver driver = new ChromeDriver(options);
      driver.get("website_URL");
      driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
      
    2. ExplicitWait example :

      WebElement button = driver.findElement(By.xpath("//input[type='submit'][contains(@value,'Add New Date')]"));
      WebDriverWait wait = new WebDriverWait(driver, 30);
      wait.until(ExpectedConditions.visibilityOf(button)).click();
      
  • An alternative you can try using .submit() method as the element is type of submit or can use JavascriptExecuter to perform the click

     JavascriptExecutor js = (JavascriptExecutor)driver;                   
     js.executeScript("arguments[0].click();", button);
    
NarendraR
  • 7,577
  • 10
  • 44
  • 82