Selenium WebDriver Clicking same element in FF and IE - selenium

Following works fine in IE, in FF it gives ElementNotVisibleException
driver.findElement(By.xpath("//div[4]/table/tbody/tr[1]/td[2]")).click();
While following works in FF, in IE it doesn't do anything:
WebElement element1 = driver.findElement(By.xpath("//div[4]/table/tbody/tr[1]/td[2]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element1);
I'm using IE 9.0 (with 32 bit IEDriverServe.exe) and FF 23.0.
Here is the Code:
<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>
I have to click Item XXXXX and Item YYYYY
div id with popup_12 and popup_23 changes every time the page is loaded or the item is clicked
Item YYYYY is displayed only when Item XXXXX is clicked
With FF using javascript executor and xpath or CSS selector it works perfectly, however with IE it doesn't
For this to work with IE I have to use xpath or CSS without javascript executor but again this time it doesn't works with FF

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

You can always see if the element is displayed (visible, not just in the DOM)
el = driver.find_element(:xpath, 'xpath').displayed?
el.click

We have tested thousands of websites and when doing so I have sometimes come across similar limitations within Selenium/WebDriver. XPath is very powerful and we use it on a daily basis however assuming the page is using JQuery, you can do the entire search of the DOM using JQuery Selector and then you don't have to worry if the element is visible or not.
The code could look something like:
driver.executeScript("$('td:contains(\"Foo\")').click();");
I don't recommend walking the DOM the way you have, there are much more efficient was find what you are looking for. However based on what you have above, you could do something like:
driver.executeScript("$($($($('body').find('div')[4]).find('table:first').find('tbody:first').find('tr')[1]).find('td')[2]).click();");
You can read more about jQuery Selectors on their webpage.
Writing scripts for functional testing is easy with the Neustar Local Validator. You can see examples and watch videos on how to do this at: http://community.neustar.biz/community/wpm/getting_started/blog/2013/11/20/neustar-training-videos
Good Luck to you.
Brian Kranson
Neustar, Inc. / Professional Services Engineer

You can use WaitTime or wait for Element as below
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();
}
}
}

Related

Correct Syntax for this Selenium command

I am just not sure what will be the syntax for this
This is a drop-down which changes its ID so I wish to choose the label above to navigate the click
Here is the HTML
<label class="form-title">Charging and Billing Alignment</label>
<select name="ratingAndBillingAlignment" class="reverse-toggle-select check-if-form-row-visible hide-child-if-not-visible loadable-drop-down-37 loadable-drop-down-select" id="ratingAndBillingAlignment" makeloadabledropdown="37" style="display: none;" fixed_position=".popup-content-inner">
<option value="TRUE" class="done-into-select-dd">Yes</option>
<option value="FALSE" selected="selected" class="done-into-select-dd">No</option>
</select><span num="37" class="loadable-drop-down loadable-drop-down-container loadable-drop-down-container-37" id="makeLoadableDropDown37" style="position: relative;" title="No" title2="No"><span class="dropdown-label initialized" style="width: 100%;"><span class="dropdown-html"><span class="value_box" style="display: none;">FALSE</span><span class="text">No</span></span></span><span style="display: none; position: absolute; width: 100%; left: 0px;" class="dropdown-list custom-drop-down-dropdown-list custom-drop-down-dropdown-list-37" stop="0" loading="0" data-counter="37"><div class="option-list" style="max-height: 250px;"><div class="dropdown-link the-dd-counter-1 first-dd-link" c="1" title="Yes"><span class="dropdown-html"><span class="value_box" style="display: none;">TRUE</span><span class="text">Yes</span></span></div><div class="dropdown-link the-dd-counter-2 last-dd-link selected" c="2" title="No"><span class="dropdown-html"><span class="value_box" style="display: none;">FALSE</span><span class="text">No</span></span></div></div></span></span>
driver.findElement(By.xpath(
"//span[#class='dropdown-label initialized']/following-sibling::label[contains(text(),'Charging and Billing Alignment')and(#xpath='1')]"))
.click();
Thanks for your help!
Here is the xpath that you can use with name. As name does not update as it did in the case of id.
//select[#name='ratingAndBillingAlignment']
Locating <select> won't work in your case as its hidden. style="display: none;"
Try using below xpath to click on span looks alike dropdown
//label[contains(.,'Charging and Billing Alignment')]/following-sibling::span[contains(#class,'loadable-drop-down')]

SeleniumIDE doesn't find my Target

Allready tried "intro to css locators in selenium" and "css selectors for selenium"but they did not bring a solution. When using Selenium IDE 2.91 to record a webpage test, playing back the scenario ends in errors. IMHO the problem is a push button with aria-pressed that changes when hovering over it, pressed or unpres it. When doing those things the div class is changing from:
<div class="gwt-PushButton A gwt-PushButton-up" tabindex="0" role="button" style="" aria-pressed="false">
To
<div....gwt-PB-up-hovering tabindex="0" role="button" style="" aria-pressed="false">
And to
<div class="gwt-PushButton A gwt-PushButton-down-hovering" tabindex="0" role="button" style="" aria-pressed="true">
Rest of html code within the div is:
<input tabindex="-1" role="presentation" style="opacity: 0; height: 1px; width: 1px; z-index: -1; overflow: hidden; position: absolute;" type="text">
<div class="html-face">Controle</div>
</div>
Record of selenium is 2 records:
1 click //div[5]/div[2]
2 click //div[5]/div[2]/div
The info selenium gives = [info] Executing: |click | //div[5]/div[2] |
| But because the button isnt really clickt on the next step goes in
error.
Any suggestions how to be able to make selenium click the button?
Already tried click using css=div.gwt-PushButton.GBKHVCADO2

Dynamic Element handling

In our application there is a task view page. For different tasks, xpath for the same button found change every time as below
WIN_1_1000000014
WIN_2_1000000014
WIN_3_1000000014
WIN_4_1000000014
WIN_5_1000000014
I am providing html codes for two of such scenarios below . I am using following xpaths to find that button but not working for me.
//div[#ardbn='Assignee Organization']//a[#class = 'btn btn3d menu']//img
//img[#alt='Menu for Organization']
Help me guys
<div id="WIN_1_1000000014" class="df arfid1000000014 ardbnAssigneeOrganization Char" arid="1000000014" artype="Char" ardbn="Assignee Organization" arlbox="0,4,78,17" ardcf="1" style="z-index:998;top:84px; left:5px; width:263px; height:21px;" arwindowid="1">
<label id="label1000000014" class="label f6" for="x-arid_WIN_1_1000000014" style="top: 4px; left: 0px; width: 78px; height: 17px;">Organization</label>
<textarea id="arid_WIN_1_1000000014" class="text sr " cols="20" maxlen="60" style="top:0px; left:83px; width:154px; height:21px;" armenu="CTM:SGP:SupportOrganization3-Q" mstyle="2" arautoc="1" arautocmb="1" arautocak="0" arautoctt="400" rows="1" title="Service_Desk_First_Tier"
wrap="off"></textarea>
<a class="btn btn3d menu" href="javascript:" style="top:0px; left:242px; width:21px; height:21px;">
<img class="btnimg" src="../../../../resources/images/mt_sprites.gif" alt="Menu for Organization" title="" style="background-color: transparent;">
</a>
<div id="WIN_3_1000000014" class="df arfid1000000014 ardbnAssigneeOrganization Char" arid="1000000014" artype="Char" ardbn="Assignee Organization" arlbox="0,4,78,17" ardcf="1" style="z-index:998;top:84px; left:5px; width:263px; height:21px;" arwindowid="3">
<label id="label1000000014" class="label f6" for="x-arid_WIN_3_1000000014" style="top: 4px; left: 0px; width: 78px; height: 17px;">Organization</label>
<textarea id="arid_WIN_3_1000000014" class="text sr " cols="20" maxlen="60" style="top:0px; left:83px; width:154px; height:21px;" armenu="CTM:SGP:SupportOrganization3-Q" mstyle="2" arautoc="1" arautocmb="1" arautocak="0" arautoctt="400" rows="1" title="Service_Desk_Resolver"
wrap="off"></textarea>
<a class="btn btn3d menu" href="javascript:" style="top:0px; left:242px; width:21px; height:21px;">
<img class="btnimg" src="../../../../resources/images/mt_sprites.gif" alt="Menu for Organization" title="" style="background-color: transparent;">
</a>
you can write the xpath for the latest task as below:
(//div[contains(#id,'WIN_')])[last()]//img[#alt='Menu for Organization']
Use below code for click or any other operations:
List<WebElement> elements = driver.findElements(By.xpath("//img[#alt='Menu for Organization']"));
for(int i=1;i<=elements.size();++i){
elements.get(i).click();
}
xpath working for 1st 3 elements is
(//div[#ardbn='Assignee Organization']//a[#class = 'btn btn3d menu']//img)[last()]
xpath working for rest elements is
(//div[#ardbn='Assignee Organization']//a[#class = 'btn btn3d menu']//img)[position()<3]

Selecting drop down in selenium webdriver

I want to select a option from the drop down. The code is like below
<select id="hidBrokerId" name="hidBrokerId" style="display: none;">
<option value="" selected="selected">-- Valitse --</option>
<span class="select-sub" style="display: none; left: 0px; top: 50px; width:232px;">
<span data-value="" style="background: none repeat scroll 0% 0% rgb(232, 232, 232);">-- Valitse --</span>
<span data-value="23" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);">Mainos netissä</span> </select>
It is having display:none attribute so you need to you actions class to navigate to that element and find it.
WebElement ele = driver.findElement(By.xpath("//select[#id='hidBrokerId' and contains(#style,'display: none')]"));
Actions act = new Actions(driver);
act.moveToElement(ele).click(driver.findElement(By.xpath("//option[contains(text(),'Mainos netissä')]"))).build().perform();
Let me know if this works

Selenium IE Webdriver

I am working on Selenium java scripts, using IE webdriver script I can identify an element but .click() is not working. Is there any other way to press ENTER against that webelement. After clicking this element one drop down appears; I want to select one element from that drop down.
Help is appreciated.
WebElement sve = driver.findElement(By.xpath("//table[#id='rptReportViewer_ctl05_ctl04_ctl00_Button']"));
if( sve.isDisplayed()){
sve.click();
System.out.println("element found");
} else {
System.out.println("element not found");
}
below is the button ids;
font-size: 10pt; border: 1px solid rgb(51, 102, 153); background-color:
rgb(221, 238, 247); cursor: pointer;">
<table id="rptReportViewer_ctl05_ctl04_ctl00_Button" border="0"
title="Export drop down menu">
<tbody>
<tr>
<td>
<a id="rptReportViewer_ctl05_ctl04_ctl00_ButtonLink" style="text-decoration:
none; cursor: pointer;" href="javascript:void(0)" alt="Export drop down
menu" title="Export drop down menu">
<img id="rptReportViewer_ctl05_ctl04_ctl00_ButtonImg" style="border-
style:None;height:16px;width:16px;border-width:0px;" alt="Export drop down
menu" src="/Reserved.ReportViewerWebControl.axd?
OpType=Resource&Version=11.0.2802.16&Name=Microsoft.Reporting.WebForms.Icons.Export.gif"/>
<span style="width:5px;text-decoration:none;"/>
<img id="rptReportViewer_ctl05_ctl04_ctl00_ButtonImgDown" style="border-
style:None;height:6px;width:7px;border-width:0px;margin-bottom:5px;"
alt="Export drop down menu" src="/Reserved.ReportViewerWebControl.axd?
OpType=Resource&Version=11.0.2802.16&Name=Microsoft.Reporting.WebForms.Icons.ArrowDown.gif"/>
</a>
</td>
</tr>
</tbody>
</table>
</div>