[英]How to click on the element using Selenium Webdriver
I'm trying to click on a pop-up alert message on a UI with Selenium Webdriver. 我正在尝试使用Selenium Webdriver在UI上单击弹出警报消息。
The problem is, it is not clicking on accept or cancel even if I explicitly and implicitly wait. 问题是,即使我明确地和隐含地等待,也没有点击接受或取消。 Is there any other alternative to clicking on a pop-up message.
有没有其他替代方法来点击弹出消息。 I tried to send key by Robot and press enter, but it did not work too.
我试图通过机器人发送密钥并按回车键,但它也没有用。
click ok popup message function: 点击确定弹出消息功能:
try {
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
report.log(LogStatus.INFO, "Displayed Pop-up Window Alert Message -> " + alert.getText() + " for the Field -> " + fieldName);
System.out.println("Displayed Pop-up Window Alert Message -> " + alert.getText() + " for the Field -> " + fieldName);
alert.accept();
Thread.sleep(3000);
} catch (NoAlertPresentException ex) {
System.err.println("Error came while waiting for the alert popup. ");
report.log(LogStatus.INFO, "Alert pop up box is NOT populating when user clicks on: ");
}
this is what the html looks like for the popup: 这是弹出窗口的html:
<input type="submit" name="ctl00$ctl00$Content$ContentPlaceHolderMain$Continue" value="Continue..."
onclick="if(warnOnDelete('ctl00_ctl00_Content_ContentPlaceHolderMain_EditRadioOptions_1',"
+ "'Please confirm if you wish to delete.') == false) return false;"
id="ctl00_ctl00_Content_ContentPlaceHolderMain_Continue" style="width:100px;">
It has to be in IE, we are not allow to use anything except IE 它必须在IE中,我们不允许使用IE之外的任何东西
Update: function for the confirm boxes 更新:确认框的功能
function warnOnDelete(deleteButtonID, msg) {
var deleteRadioButton = document.getElementById(deleteButtonID);
if (deleteRadioButton != null) {
if (deleteRadioButton.checked == true)
return confirm(msg);
}
return true;
}
Seems the element is not an Alert
but an <input>
element and to click()
on the element you have to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies : 看起来元素不是
Alert
而是<input>
元素,并且要在元素上click()
,您必须为elementToBeClickable()
引入WebDriverWait ,并且您可以使用以下任一定位器策略 :
cssSelector
: cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[id$='_Content_ContentPlaceHolderMain_Continue'][value^='Continue'][name$='Continue']"))).click();
xpath
: xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(@id, '_Content_ContentPlaceHolderMain_Continue') and starts-with(@value, 'Continue')][contains(@name, 'Continue')]"))).click();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.