【问题标题】:Selenium finding element in a javascript popupSelenium 在 javascript 弹出窗口中查找元素
【发布时间】: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


【解决方案1】:

有时,我们无法使用 webDriver 访问弹出窗口中的元素,在这种情况下,我们可以使用 JavascriptExecutor 来查找该元素。您似乎知道元素的“Id”,请尝试以下代码以访问该元素。

WebElement ele =(WebElement) ((JavascriptExecutor)driver).executeScript("return document.getElementById("yourElementId"));

【讨论】:

  • 我试过这个:WebElement ele =(WebElement) ((JavascriptExecutor)webDriver).executeScript("return document.getElementById('1')"); ele.click();但在点击时得到 NullPointerException
  • 先切换到alert再试试
  • 如果要检查iframe是否存在,可以使用如下代码 List iframes = driver.findelements(by.tagname("iframe"'));检查像 iframes.size() 这样的列表的 size() 以确保 iFrame 的存在。
猜你喜欢
  • 1970-01-01
  • 2013-01-16
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多