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>
Related
Clicking on the anchor tag opens a menu and in one of the options(named Application) there is another menu where few applications are listed and I have to click on them. I am unable to click on it since it is not wrapped in a select tag.
here is the code:
<div class="menuBarSub mb_standardSub" menu="mn-1441711622465-/contextMenu" style="position: absolute; left: 1252.33px; top: 17px; display: block; z-index: 100;" role="navigation">
<table cellspacing="0" cellpadding="0">
<tbody role="menu" style="width: 100%;">
<tr id="/contextMenu/ID1277705525089001" class="menuItem" orientation="vertical">
<tr id="/contextMenu/ID1285350191126000" class="menuItem" orientation="vertical">
<tr id="/contextMenu/ID1412292365029000" class="menuItem" orientation="vertical">
<tr id="/contextMenu/ID1416325132235000" class="menuItem selected" orientation="vertical" aria-selected="true">
<tr id="/contextMenu/ID1332408920353004" class="menuItem" orientation="vertical">
</tbody>
</table>
</div>
Few points:
1. After clicking on the anchor tag you will have to wait for the presence of the menu. Use WebDriverWait and ExpectedConditions class for that.
2. You will have to hover over the menu appearing using Actions class clickAndHold(Locator).build().perform() method.
3. The get(0) is a different issue and just so that you should know it has nothing to do with selenium but has to do with Java. get(0) is method from List<E> collections Interface which is returned by WebDriver#findElements(Locator) API.
Let me know if you need further assistance.
I have to select the above highlighted link New V1 Project but I am not able to identify the element.
I've tried using linktext function but it is giving error... can anybody help?
Hierachy is something like:-
<tbody>
<tr>
<td width="40" align="center">
<td width="5">
<td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc">
<a class="navSelected" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.view&version=1">
<td width="5">
<td width="5">
<td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc">
<a class="nav" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.create_form&version=1">
<td width="5">
<td width="5">
<td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc">
<a class="nav" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.create_form&version=2">
</tr>
</tbody>
please see the highlighted HTML code in image... I am not sure how this Text element is linked with a tag
My Code that I've used to identify element is:
WebElement v1Link = driver.findElement(By.linktext("New V1 Project"));
WebElement v1Link = driver.findElement(By.xpath("//tr/td[3]/a[#text()= 'New v1 Project']"));
but none of the above 2 methods are working... please somebody help.
You can find the link by the following xpath:
//td/a[normalize-space(text()) = 'New V1 Project']
FYI, your version of xpath was almost correct:
no need to put # before the text()
the text is New V1 Project, not New v1 Project
you need to use normalize-space()
Hope that helps.
Try using cssselector-
WebElement v1Link = driver.findElement(By.cssSelector("a.nav[href='index.cfm?fuseaction=project.create_form&version=2']"));
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();
}
}
}
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();
code is used is:
WebElement desc=driver.findElementByXPath(".//*[#label='Description']");
desc.sendKeys("testing");
desc.sendKeys(Keys.ENTER);
List<WebElement> button=driver.findElementsByXPath("(//div[#id='sv'])[1]");
for (WebElement buttonname : button)
{
System.out.println("buttonname: "+buttonname.getAttribute("id"));
String but = buttonname.getAttribute("id");
driver.findElementById(but).click();
}
Below is the html code of that textarea and button .
<td>
<textarea id="1992800000" label="Description" ft="12" mand="false"class="ic" maxlength="120" cols="13" rows="2"/>
</td>
......
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 40px; ">
<td class="pdl">
<div class="tbut" onclick="ir('Tas','tas','')" id="sv">Save</div>
</td>
Your XPath can only select one element, so there is no need to create a list and iterate through it.
Try something more like:
WebElement desc=driver.findElementByXPath("//*[#label='Description']");
desc.sendKeys("testing");
WebElement button=driver.findElementsByXPath("(//div[#id='sv'])[1]");
button.click();