webdriver why the table can get its size but canot get its text - selenium

the pagesource:
<TABLE class=mini-listbox-items style="WIDTH: 100%" cellSpacing=0 cellPadding=0>
<TBODY>
<TR class="mini-listbox-item mini-listbox-item-selected" id=mini-45$0 index="0">
<TD class=mini-listbox-checkbox><INPUT id=mini-45$ck$0 type=checkbox CHECKED></TD>
<TD class="">xx</TD></TR>
<TR class=mini-listbox-item id=mini-45$1 index="1">
<TD class=mini-listbox-checkbox><INPUT id=mini-45$ck$1 type=checkbox></TD>
<TD class="">yy</TD></TR></TBODY></TABLE></DIV><INPUT type=hidden value=Y></DIV>
<DIV class=mini-errorIcon></DIV></DIV></DIV>
<DIV class=mini-shadow style="DISPLAY: none; Z-INDEX: 1002; LEFT: 393px; WIDTH: 78px; TOP: 326px; HEIGHT: 46px"></DIV></BODY>
my code:
driver.findElements(By.xpath(("//td[#class='mini-listbox-checkbox']/following-sibling::td[position()=1]"))).size();
driver.findElements(By.xpath(("//td[#class='mini-listbox-checkbox']/following-sibling::td[position()=1]"))).get(0).getText();
the first line,it prints 2,then the second line,it prints none,why?

Your logic is working fine.
int n=driver.findElements(By.xpath(("//td[#class='mini-listbox-checkbox']/following-sibling::td[position()=1]"))).size();
String s=driver.findElements(By.xpath(("//td[#class='mini-listbox-checkbox']/following-sibling::td[position()=1]"))).get(0).getText();
System.out.println(n+"======>>"+s);
The above code is giving correct output only.
2======>>xx

Related

nowrap on td in table but not if colspan

Is it possible to control nowrap of table cells depending on if the cell (td) has a colspan attribute or not?
Current css
.k2table {table-layout: fixed; width:100%;border-collapse: collapse}
.k2table tr {height:18px}
.k2table td {text-align: left;padding:1px;white-space: nowrap;}
.k2table td+td {text-align: right;width:70px;}
.k2table td+td+td {text-align: right;width:70px;}
If I have a table such as below I don't want nowrap on the td with colspan.
<table class="k2table">
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="3"></td>
</tr>
</table>
You can do that using an [attr] selector
.k2table td[colspan] { white-space: normal }
If you need to be more specific you can apply your style just to the cell with THAT colspan
.k2table td[colspan="3"] { white-space: normal }

PHP selenium find parent sibiling

I have below code and I want find second td value. How can I select text with <br/> in it?
<tr>
<td valign="top" style="width: 85px">
<span class="fieldtext">Address:</span>
</td>
<td valign="top">
Shaftesbury House, 1st floor
<br/>20 Tylney Road
<br/>Bromley
<br/>Greater London
<br/>BR1 2RL
<br/>United Kingdom
<br/>
</td>
<td style="width: 200px; vertical-align: top; text-align: right;" rowspan="2" />
</tr>
Assuming you want to find the td element by the Address: label defined previously, you can use the following-sibling axis:
//td[span = 'Address:']/following-sibling::td
Then, after locating the element, call getText() method:
$driver->findElement(WebDriverBy::xpath("//td[span = 'Address:']/following-sibling::td"))->getText();
Try as below :-
$driver->findElement(WebDriverBy::xpath('//tr/td[2]'))->getText();
Hope it helps....:)

HTML table styling and layout

I'm trying to create a basic layout or four tables which sit side by side (with a small margin between the tables. Each table will have one row in the top section containing a currency symbol which will stretch the full length of the table and then below a second row will be subdivided into two cells with an input box and submit button.
There are two issues which I can't seems to find a solution for:
The top row divides into two cells. This means when the symbol is centered it centers to the left cell as opposed to the centre of the table (top row should stretch the full width of the table)
The row/ cells below contain a large amount of empty space above the input box and button which I can't seem to remove (have tried setting margin / borders to 0 etc.
Here is the code (borders added for clarity):
HTML
<table id = "pound_table">
<tr>
<td id = "pound_symbol">£</td>
</tr>
<tr id = "input">
<td id = "input"><input type = "text" name = "pound" size = "6" /></td>
<td><input type = "submit" value = "CONVERT" /></td>
</tr>
</table>
<table id = "euro_table">
<tr>
<td id = "euro_symbol">€</td>
</tr>
<tr>
<td><input type = "text" name = "euro" size = "6" /></td>
<td><input type = "submit" value = "CONVERT" /></td>
</tr>
</table>
<table id = "dollar_table">
<tr>
<td id = "dollar_symbol">$</td>
</tr>
<tr>
<td><input type = "text" name = "dollar" size = "6" /></td>
<td><input type = "submit" value = "CONVERT" /></td>
</tr>
</table>
<table id = "yen_table">
<tr>
<td id = "yen_symbol">¥</td>
</tr>
<tr>
<td><input type = "text" name = "yen" size = "6" /></td>
<td><input type = "submit" value = "CONVERT" /></td>
</tr>
</table>
CSS
table
{
border-spacing: 0px;
border: 1px solid black;
margin-right:20px;
}
tr {
margin: 0;
padding:0;
border-spacing: 0px;
border: 1px solid black;
}
td {
border: 1px solid black;
margin: 0;
padding:0;
border-spacing: 0px;
}
#pound_symbol,
#euro_symbol,
#dollar_symbol,
#yen_symbol
{
text-align: center;
}
#pound_table,
#euro_table,
#dollar_table,
#yen_table
{
display: inline-block;
font-size:72px;
}
also see the fiddle;
http://jsfiddle.net/qs0dehhx/1/
Fixed, take a look: https://jsfiddle.net/zer00ne/aonc6we7/1/
HTML
For the the first row: <td colspan="2" id=...>
CSS
input { display: block; min-height: 100%; }

Getting Error while identifying an element in Selenium WebDriver

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']"));

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