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

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();
*/

Related

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

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>

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

Selenium Webdriver - (Java) - Click a button with dynamic ID

1) I have a dialog on my web page having 2 buttons, Yes & No.
2) IDs of these buttons are dynamicaly changing every time.
3) How to handle this situation and click on Yes button?
4) Both buttons, Yes & No, have same classname 1.e. rwInnerSpan
5) Here is the Xpath for Yes button
(.//*[#id='confirm1381468352443_content']/div/div[2]/a[1]/span/span)
the part 1381468352443 in xpath is dynamically changing.
Below is the source code of page
`
<tr class="rwTitleRow">
<tr class="rwContentRow">
<td class="rwCorner rwBodyLeft"> </td>
<td class="rwWindowContent" valign="top">
<iframe frameborder="0" name="confirm1381468352443" src="javascript:'<html></html>';" style="width: 100%; height: 100%; border: 0px none; display: none;" tabindex="0">
<div id="confirm1381468352443_content">
<div class="rwDialogPopup">
<div class="rwDialogText">
<div>
<a class="rwPopupButton" href="javascript:void(0);" onclick="$find('confirm1381468352443').close(true);" tabindex="-1">
<span class="rwOuterSpan">
<span class="rwInnerSpan">Yes</span>
</span>
</a>
<a class="rwPopupButton" href="javascript:void(0);" onclick="$find('confirm1381468352443').close(false);" tabindex="-1">
<span class="rwOuterSpan">
<span class="rwInnerSpan">No</span>
</span>
</a>`
Thanks in Advance !!
You can directly check for the text in your Xpath:
driver.findElements(By.xpath("//a[#class='rwPopupButton']/span/span[contains(text(), 'Yes')]"))
There is a way to locate objects using partial link text, so you can try this:
driver.findElement(By.partialLinkText("Yes")).click();
Plain By.linkText may not work because of additional spaces or characters in the link.
You can click on the button based on the Text. Following method will give you a webelement based on the class locator and text.
WebElement getElementBasedOnClassAndText(String classLocator, String text){
List<WebElement> elements = driver.findElements(By.className(classLocator))
for(WebElement element : elements){
if(element.getText().contentEquals(text)){
return element
}
}
Assert.fail("Unable to find any element with the said Text")
}
Once you get the element you can take any action on it.
Since it is dynamic I would look for the changed name:
//Find the dynamicly created ID
String dynamicID = driver.findElement(By
.xpath("//iframe[contains(#name,'confirm')]")
.getAttribute("name");
//Use that ID to find the Yes option
driver.findElement(By
.xpath("//*[#id='"+dynamicID +"_content']/div/div[2]/a[1]/span/span")
.click();