0

I have tried many ways but it did't work for me. please help on this

DOM area:

<td>
<input name="ctl00$cntMainContent$txtStartDate" type="text" id="ctl00_cntMainContent_txtStartDate" class="clsTextBox" onkeydown="javascript:return false;" onpaste="javascript:return false;">                                
<img id="ctl00_cntMainContent_imgStartDate" src="../Images/calendar.gif" align="absbottom">
</td>

below are the areas i have tried, Normal Click method:

driver.findElement(By.xpath("//*[@id='ctl00_cntMainContent_imgStartDate']")).click();

Javascriptexecutor:

public void Javascript_Click()
    {           
        JavascriptExecutor executor= (JavascriptExecutor)driver;            executor.executeScript("document.getElementById('ctl00_cntMainContent_imgStartDate').click()");     
    } 

Actions:

public void Action_Click()
        {
            Actions act = new Actions(driver);          act.moveToElement(driver.findElement(By.id("ctl00_cntMainContent_imgStartDate"))).click().build().perform();
        }
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Satya
  • 1
  • 2

2 Answers2

0

As the element is a JavaScript enabled element so to click() on the element you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td img[id$='cntMainContent_imgStartDate'][src$='Images/calendar.gif']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td//img[contains(@id, 'cntMainContent_imgStartDate') and contains(@src, 'Images/calendar.gif')]"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • i tried this..but not worked for me. showing no error and no failures – Satya Jun 15 '20 at 05:41
  • @DebanjanB...Any other ways to interact with that element – Satya Jun 17 '20 at 06:26
  • @Satya You only told us _it did't work for me_ but you haven't told us what went wrong or shared the error stack trace. So it's difficult to construct a canonical answer. – undetected Selenium Jun 17 '20 at 06:28
  • It is not throwing any error for those click statements – Satya Jun 17 '20 at 11:37
  • When i highlight the element using Javascriptexector it is highlighting but not clicking if we write the click method....really awaiting the suggestions to solve this...r – Satya Jun 17 '20 at 11:40
0

Try following approach:

  1. With Actions

    WebElement element = driver.findElement(By.cssSelector("td img#ctl00_cntMainContent_imgStartDate"));
    Actions actions = new Actions(driver);
    actions.moveToElement(element)
        .click(element)
        .build()
        .perform();
    
  2. With JavascriptExecutor

    WebElement element = driver.findElement(By.cssSelector("td img#ctl00_cntMainContent_imgStartDate"));
    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
    
frianH
  • 7,295
  • 6
  • 20
  • 45