硒在javascript弹出窗口中查找元素 [英] Selenium finding element in a javascript popup

查看:121
本文介绍了硒在javascript弹出窗口中查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单。我需要点击由javascript代码生成的弹出框中的元素。该页面仅在IE中可用,并且在开发人员工具处于打开状态时,弹出窗口中的元素无法选择。基本上我不知道xpath,但我确实有生成弹出窗口的JavaScript。这是网页上的弹出式菜单:





是生成弹出窗口的JavaScript代码:
$ b $ pre $ 函数pickDefaultType(){
//创建弹出
thePopup = window.createPopup();
var doc = thePopup.document;
var body = doc.body;

//添加样式表
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;);

//创建滚动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;

//确定大小以显示弹出窗口
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等待=新WebDriverWait(webDriver,3); 
WebElement elem = wait.until(ExpectedConditions.elementToBeClickable(By.id(0)));

我也尝试过Selenium的Alert类:

 警报promptAlert = webDriver.switchTo()。alert(); 
字符串alertText = promptAlert.getText();
System.out.println(Alert text is+ alertText);

无法找到该元素。该警报不存在。



如果弹出窗口位于框架中,我如何找到它的名称以切换到它?



请注意,我已经使用Selenium成功打开了下拉菜单,但找不到要点击它们的元素。



谢谢

解决方案

有时,我们无法使用webDriver访问元素,在这种情况下,我们可以使用JavascriptExecutor找到那个元素。看起来你知道元素的Id,请尝试下面的代码来访问元素。

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


The problem is simple. I need to click an element in a popup that is generated by javascript code. The page is only available in IE and the elements in the popup cannot be selected when Developers tools is on. Basically I do not know the xpath but I do have the javascript that generates the popup. This is the popup on page:

And this is the javascript code that generates the popup:

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);
}

I have tried locating the element "None" with xpath and id, I used wait as well, e.g.

WebDriverWait wait = new WebDriverWait(webDriver, 3);
WebElement elem = wait.until(ExpectedConditions.elementToBeClickable(By.id("0")));

I have also tried the Alert class from Selenium:

Alert promptAlert = webDriver.switchTo().alert();
    String alertText = promptAlert.getText(); 
    System.out.println("Alert text is " + alertText);

The element cannot be found. The Alert is not present.

If the popup is in a frame, how do I find out the name of it to switch to it?

Please note that I have successfully opened the dropdown using Selenium, just cannot find the elements on it to click on them.

Thank you in advance!

解决方案

Sometimes, we are not able to access element in pop up using webDriver, in that case we can use JavascriptExecutor to find that element. It seems that you know the "Id" of the element, please try below code to get access to the element.

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

这篇关于硒在javascript弹出窗口中查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆