Shoaib Shaikh
Shoaib Shaikh

Reputation: 353

Click on a button not working

I'm using selenium webdriver with java.

I have encountered a situation where i am able to locate & click on a button,but nothing happens after this. The HTML code for the said button is->

<div id="divAllButtons" class="UCButtonMainCSS" style="display: none;">
<div>
<div id="OtherActionParent" class="mT8">
<div id="btnSave" class="btn fLt mR20">
<span>
<a onclick="Save_onclick()" href="javascript:void(0)">
<span id="Label24">Save</span>
</a>
</span>
</div>

The button when clicked, should redirect to the confirmation page, or show an alert message if mandetory fields are not filled. I have tried few thing,

1

Button = driver.findElement(By.id("btnSave"));
Button.click();

2

Button = driver.findElement(By.xpath("//div[@id='dataContainer']/div[2]/div/div/div[4]/div/div[1]/div[2]/span/a"));
Button.click();

3

Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//div[@id='dataContainer']/div[2]/div/div/div[4]/div/div[1]/div[2]/span/a"));
action.moveToElement(we).click().build().perform();

4

Point coordinates = driver.findElement(By.xpath("//div[@id='dataContainer']/div[2]/div/div/div[4]/div/div[1]/div[2]/span/a")).getLocation();
Robot robot = new Robot();
robot.mouseMove(coordinates.getX()+40,coordinates.getY()+30);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);

Each of the way appears to work fine to click on the button as message javascript:void(0) is displayed in the bottom corner of the browser.

Upvotes: 1

Views: 1429

Answers (4)

Shoaib Shaikh
Shoaib Shaikh

Reputation: 353

The problem is solved. The issue was not to locate the element but the onClick() event was not firing. Then i found out that something else was there which stopped from the event to fire. I had used java script to enable the date picker box & did this,

((JavascriptExecutor)driver).executeScript ("document.getElementById('txtOriginDate').removeAttribute('readonly',0);");

WebElement originDateBox= driver.findElement(By.xpath(prop.getProperty("originDateBox")));
originDateBox.clear();
originDateBox.sendKeys("9-Dec-2014"); //Enter date 

Developer designed this in such a way that if you don't use date picker to select date, a specific variable was not set. Which eventually made the onclick event not to fire.

The date picker code was something like this,

var jsoncustdate = "";
var jsonorigindate = "";

function onSelectCalender( StrDt, obj ) 
{
    if ( !varReadonly )
    {
        if ( $( "#txtCustDescisionDate" ).attr( "IsDisable" ) == "FALSE" ) 
        {
            if ( obj.id == "txtCustDescisionDate" ) 
            {
                custobjDt = new Date( obj.selectedYear, obj.selectedMonth,obj.selectedDay, 0, 0, 0, 0 );
                jsoncustdate = custobjDt.getTime();
                jsoncustdate = "\/Date(" + jsoncustdate + ")\/";
                DisabledBtnStage();
        //      $("#txtFromDate").datepicker("option", "maxDate", objDt);

            }
            if ( obj.id == "txtOriginDate" ) 
            {
                 var objDt = new Date( obj.selectedYear, obj.selectedMonth,obj.selectedDay,0, 0,0,0 );
                 jsonorigindate = objDt.getTime();
                 jsonorigindate = "\/Date(" + jsonorigindate + ")\/";
                 DisabledBtnStage();
        //       $("#txtToDate").datepicker("option", "minDate", objDt);
            }
       }
      elogCommon.CheckMandatory();
   }

}

I finally used date picker in normal way & the event fired smoothly.

Thank you guys for help . .cheers !!!

Upvotes: 0

Pradeep
Pradeep

Reputation: 21

Just try with javascript executor. it may work....

WebElement save = driver.findElement(By.id("btnSave")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", save);

Upvotes: 1

Saifur
Saifur

Reputation: 16201

Try a text based xpath search

text based xpath solved a lot of issues for me. You just need to make sure you have enough wait time before clicking the element

EDIT: try using action

By saveButton = By.xpath("//*[.='Save']");
WebElement element = driver.findElement(saveButton); 
Actions action = new Actions(driver); 
action.moveToElement(element).build().perform(); 
driver.findElement(saveButton).click();

Note: untested code written in java

Upvotes: 1

Subh
Subh

Reputation: 4424

Try this code to click on the Save button and see if it works:

WebElement Button = driver.findElement(By.xpath("//div[@id='btnSave]//a"));
Button.click();

OR

WebElement Button = driver.findElement(By.xpath("//div[@id='btnSave]//span[.='Save']"));
Button.click();

Upvotes: 1

Related Questions