以下在IE中工作正常,在FF中它给出了ElementNotVisibleException
driver.findElement(By.xpath("//div[4]/table/tbody/tr[1]/td[2]")).click();
虽然以下在FF中有效,但在IE中它没有做任何事情:
WebElement element1 = driver.findElement(By.xpath("//div[4]/table/tbody/tr[1]/td[2]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element1);
我正在使用IE 9.0
(使用32位IEDriverServe.exe)和FF 23.0
。
以下是代码:
<div id="popup_12" class="dijitPopup dijitMenuPopup" style="z-index: 1000; right: auto; left: 45px; top: 27px; visibility: visible; display: none;" role="presentation" dijitpopupparent="dijit_MenuBar_0">
<table class="dijit dijitMenu dijitReset dijitMenuTable dijitMenuPassive" cellspacing="0" data-dojo-attach-event="onkeypress:_onKeyPress" tabindex="0" role="menu" widgetid="dijit_Menu_1" style="top: 0px; visibility: visible;">
<tbody class="dijitReset" data-dojo-attach-point="containerNode">
<tr class="dijitReset dijitMenuItem" tabindex="-1" role="menuitem" data-dojo-attach-point="focusNode" aria-labelledby="dijit_PopupMenuItem_1_text dijit_PopupMenuItem_1_accel" style="-moz-user-select: none;" widgetid="dijit_PopupMenuItem_1" aria-haspopup="true">
<td class="dijitReset dijitMenuItemIconCell" role="presentation">
<td id="dijit_PopupMenuItem_1_text" class="dijitReset dijitMenuItemLabel" data-dojo-attach-point="containerNode" colspan="2">XXXXX</td>
<div id="popup_23" class="dijitPopup dijitMenuPopup" style="z-index: 1001; right: auto; left: 117px; top: 0px; visibility: visible; display: none;" role="presentation" dijitpopupparent="dijit_Menu_1">
<table class="dijit dijitMenu dijitReset dijitMenuTable dijitMenuPassive" cellspacing="0" data-dojo-attach-event="onkeypress:_onKeyPress" tabindex="0" role="menu" widgetid="dijit_Menu_2" style="top: 0px; visibility: visible;">
<tbody class="dijitReset" data-dojo-attach-point="containerNode">
<tr class="dijitReset dijitMenuItem" tabindex="-1" role="menuitem" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_3_text dijit_MenuItem_3_accel" style="-moz-user-select: none;" widgetid="dijit_MenuItem_3">
<tr class="dijitReset dijitMenuItem" tabindex="-1" role="menuitem" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_4_text dijit_MenuItem_4_accel" style="-moz-user-select: none;" widgetid="dijit_MenuItem_4">
<tr class="dijitMenuSeparator" style="-moz-user-select: none;" widgetid="dijit_MenuSeparator_0">
<tr class="dijitReset dijitMenuItem" tabindex="-1" role="menuitem" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_5_text dijit_MenuItem_5_accel" style="-moz-user-select: none;" widgetid="dijit_MenuItem_5">
<td class="dijitReset dijitMenuItemIconCell" role="presentation">
<td id="dijit_MenuItem_5_text" class="dijitReset dijitMenuItemLabel" data-dojo-attach-point="containerNode" colspan="2">YYYYY</td>
我必须点击项目XXXXX和项目YYYYY
每次加载页面或点击项目时,带有popup_12和popup_23的div id都会更改
仅在单击项目XXXXX时显示项目YYYYY
使用javascript执行器和xpath或CSS选择器的FF它完美地工作,但是对于IE它没有
为了使用IE,我必须使用xpath或CSS而不使用javascript执行器,但这次它不适用于FF
答案 0 :(得分:0)
您始终可以查看元素是否显示(可见,而不仅仅是在DOM中)
el = driver.find_element(:xpath, 'xpath').displayed?
el.click
答案 1 :(得分:0)
我们已经测试了数千个网站,在这样做时,我有时会遇到Selenium / WebDriver中的类似限制。 XPath非常强大,我们每天都使用它,但假设页面使用的是JQuery,你可以使用JQuery Selector对DOM进行整个搜索,然后你就不用担心元素是否可见。 / p>
代码可能类似于:
driver.executeScript("$('td:contains(\"Foo\")').click();");
我不建议以你的方式走DOM,有更高效的找到你想要的东西。但是根据您的上述内容,您可以执行以下操作:
driver.executeScript("$($($($('body').find('div')[4]).find('table:first').find('tbody:first').find('tr')[1]).find('td')[2]).click();");
您可以在webpage上阅读有关jQuery选择器的更多信息。
使用Neustar Local Validator可以轻松编写用于功能测试的脚本。您可以在以下网址查看示例并观看有关如何执行此操作的视频:http://community.neustar.biz/community/wpm/getting_started/blog/2013/11/20/neustar-training-videos
祝你好运。答案 2 :(得分:0)
XXXXX和YYYYY
int no_of_popups = driver.findElements(By.cssSelector("div[id*='popup']")).size();
for(int i=1;i<=no_of_popups;i++)
{
String text = driver.findElement(By.cssSelector("div.dijitPopup:nth-of-type("+i+
") > table.dijitReset > tbody.dijitReset > tr.dijitMenuItem > td.dijitMenuItemLabel")).getText();
S.O.P(text);
}
答案 3 :(得分:0)
您可以使用WaitTime或等待Element,如下所示
int timeout=1;
while(timeout<=60)
{
WebElement element1 = driver.findElement(By.xpath("//div[4]/table/tbody/tr[1]/td[2]"));
if(element1.isDisplayed())
{
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element1);
break;
}
else
{
try {
Thread.sleep(2000);
timeout++;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}