【发布时间】:2017-01-30 10:04:21
【问题描述】:
问题很简单。我需要在由 javascript 代码生成的弹出窗口中单击一个元素。该页面仅在 IE 中可用,并且在打开开发人员工具时无法选择弹出窗口中的元素。基本上我不知道 xpath,但我确实有生成弹出窗口的 javascript。这是页面上的弹出窗口:
这是生成弹出窗口的 javascript 代码:
function pickDefaultType() {
// create popup
thePopup = window.createPopup();
var doc = thePopup.document;
var body = doc.body;
// add stylesheet
var oSheet = doc.createStyleSheet();
oSheet.addRule("TD", "cursor: hand; font-family: Verdana, Arial, sans-serif; font-size: " + $('body').css('font-size') + ";");
oSheet.addRule(".TableStyle", "overflow-y: auto; overflow-x: visible;");
oSheet.addRule(".DivStyle", "height: 100%; overflow-y: auto; overflow-x: visible; border: #C1D3E7 1px solid; color: #404040");
oSheet.addRule(".tableHeading", "background-color: #326894; color: white;");
// create scrolling div
var theDiv = thePopup.document.createElement("DIV");
theDiv.className = "DivStyle";
body.appendChild(theDiv);
theDiv.innerHTML = "<table cellpadding='2' cellspacing='0' width='100%' height='100%'><tr><td id='0'>" + sam.appStrings.defaultDateTypeNone + "</td><tr><tr><td id='1'>" + sam.appStrings.defaultDateTypeCurrent + "</td><tr><tr><td id='2'>" + sam.appStrings.defaultDateTypeOffset + "</td><tr><tr><td id='3'>" + sam.appStrings.defaultDateTypeFixed + "</td><tr></table>";
var theTable = theDiv.firstChild;
theTable.className = "TableStyle";
theTable.style.display = "";
theTable.onclick = selectDefaultType;
theTable.onmouseover = mouseOver;
theTable.onmouseout = mouseOut;
// deterine size to show the popup
thePopup.show(10, 10, 10, 10, typeSpan);
var tableWidth = theTable.offsetWidth + 18;
var tableHeight = theTable.offsetHeight + 2;
thePopup.show(0, typeSpan.clientHeight + 2, tableWidth, tableHeight, typeSpan);
}
我尝试使用 xpath 和 id 定位元素“None”,我也使用了等待,例如
WebDriverWait wait = new WebDriverWait(webDriver, 3);
WebElement elem = wait.until(ExpectedConditions.elementToBeClickable(By.id("0")));
我也尝试过 Selenium 的 Alert 类:
Alert promptAlert = webDriver.switchTo().alert();
String alertText = promptAlert.getText();
System.out.println("Alert text is " + alertText);
找不到该元素。警报不存在。
如果弹出框在框架中,我如何找到它的名称以切换到它?
请注意,我已经使用 Selenium 成功打开了下拉菜单,只是找不到上面的元素来点击它们。
提前谢谢你!
【问题讨论】:
-
一位开发人员告诉我,.htc 文件用于呈现该弹出窗口。那么我可以访问此弹出窗口中的元素吗?
标签: javascript html selenium xpath