我在Java中使用Selenium Webdriver。
我遇到了无法定位并单击按钮的情况,但是此后没有任何反应。所述按钮的HTML代码为->
<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>
单击该按钮后,应将其重定向到确认页面,或者如果未填写强制性字段,则显示警告消息。我尝试了几件事,
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);
在浏览器的下角显示javascript:void(0)消息时,每种方法似乎都可以很好地单击按钮。
问题已经解决了。问题在于没有找到元素,但是onClick()事件没有触发。然后我发现那里还有其他东西阻止了事件的发生。我使用Java脚本启用了日期选择器框并执行了此操作,
((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
开发人员设计这种方式的方式是,如果您不使用日期选择器选择日期,则不会设置特定的变量。最终使onclick事件不触发。
日期选择器代码是这样的,
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();
}
}
我终于以正常方式使用日期选择器,并且事件顺利启动。
谢谢你们的帮助。欢呼!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句