Selecting drop down in selenium webdriver - selenium

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

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]

Selenium WebDriver: How to click icon(right edge) of split-button to see dropdown list using Selenium WebDriver with java?

How to click the icon in a split button in order to see the drop down list in the following UI code.
<div id="splitbutton-1081" class="x-btn x-box-item x-btn-default-small x-noicon x-btn-noicon x-btn-default-small-noicon x-menu-active x-btn-menu-active x-btn-default-small-menu-active x-over x-btn-over x-btn-default-small-over over" style="margin: 0px; padding: 2px 10px; background-color: rgb(109, 172, 236); background-image: none; border: 1px solid rgb(36, 91, 147) ! important; left: 474px; top: 0px;">
<em id="splitbutton-1081-btnWrap" class="x-btn-split x-btn-split-right">
<button id="splitbutton-1081-btnEl" class="x-btn-center" autocomplete="off" role="button" hidefocus="true" type="button" style="height: 16px;">
<span id="splitbutton-1081-btnInnerEl" class="x-btn-inner" style="">Estimate</span>
<span id="splitbutton-1081-btnIconEl" class="x-btn-icon "/>
</button>
</em>
</div>
Here is my codes. The drop-down list doesn't seem to show.
In NetworkPageObject.java file:
#FindBy(xpath = "//*[starts-with(#id, 'splitbutton')]/descendant::span[contains(text(),'Estimate')]")
#CacheLookup
private WebElement EstimateDropList;
/* I also tried 2 different xpath as follows, but drop-down list still not show. i.e.
#FindBy(xpath = "//*[starts-with(#id, 'splitbutton')]/descendant::span[contains(text(),'Estimate')]/following-sibling::span[#class='x-btn-icon '][1]")
#FindBy(xpath = "//*[starts-with(#id, 'splitbutton')]/descendant::span[contains(#class, 'x-btn-icon ')]")
*/
public void click_EstimateDropList() {
ExpectedConditions.visibilityOf(EstimateDropList);
EstimateDropList.click();
public WebElement get_EstimateDropList() {
return EstimateDropList;
}
In NetworkTest.java file:
#Test(description="Test if user can click Estimate drop list")
public void testClickNetworkEstimate () {
NetworkPageObject networkPageObj =PageFactory.initElements(driver,
NetworkPageObject.class);
networkPageObj.click_EstimateDropList();
/* I also tried the following, but no drop-down will show
Actions build = new Actions(driver);
build.moveToElement(networkPageObj.get_EstimateDropList())
.release(networkPageObj.get_EstimateDropList()).build().perform();
*/

Selenium WebDriver Clicking same element in FF and IE

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