Webdriver findelements does not pick up inner html - webdriver

I am trying to use the following code to extract the inner html of menu items but it only picks up the text that is in the same line for some reason. Here is the code:
WebElement ReportStart=driver.findElement(By.xpath("//a[contains(text(),'Reports')]"));
List<WebElement> reports= ReportStart.findElements(By.xpath("//*[starts-with(#id, 'menu_')]"));
System.out.println(reports.size());
for (int i = 0; i < reports.size(); i++) {
System.out.println(reports.get(i).getAttribute("id"));
System.out.println(reports.get(i).getText());
}
It picks up all the id's but only the inner text of some of them and not nested one. So it picks up text Reports and id menu_1035 and menu_1036 but not the text General as it is not on the same line. Not sure why this is that findelements does not pick the whole web element
<li>
<a id="menu_1035" class="dynmenu" href="#">Reports</a>
<ul>
<li>
<a id="menu_1036" href="#">
General
<span class="dwn"/>
</a>
<ul>
</li>

Its not picking the text General as it is inner-html of li tag not a tag.
Your xpath only fetches element which has id - //*[starts-with(#id, 'menu_')]
If you want to get General text also, then you need to update your xpath' that points only tillli` element.
Hope it helps.

Related

actionlink manipulates html render order in nested anchor

I'm having a problem and i'm really puzzled by it.
My markup is simple enough:
#foreach (var item in Model.Items)
{
<a class="mapIconUnit" id="pinDelete-#item.PinGuid.ToString()">
#Url.Action("DeletePin") <!-- testing purposes -->
#(Ajax.ActionLink("x", "DeletePin", MapAdministrationController.Routes.DeletePin(item.PinGuid), new AjaxOptions()
{
OnSuccess = "onMapPinDeleted",
Confirm = Resources.Resource.msg_GenericDeleteConfirmationQuestion
}
))
</a>
}
Now what i would expect to render from this is:
<a class="mapIconUnit" id="...">
... rendered url
<a href="..." etc>x</a>
</a>
But what i am getting is:
<a class="mapIconUnit" id="...">
... rendered url
</a>
<a href="..." etc>x</a>
What am i doing wrong here? The markup is too simple for it to be wrong to cause such a thing?
It's illegal to nest an anchor element inside another anchor element, more info can be found in the W3C specs: http://www.w3.org/TR/html401/struct/links.html#h-12.2.2
Links and anchors defined by the A element must not be nested; an A element must not contain any other A elements.
So either razor or the webbrowser renders the elements correctly (i.e. place them next to each other).

diplay title of href and id div in the same line

I want to make title of href and id of div in the same line
this is my code :
<a href="#">my link name <div id="iddiv"></div>
</a>
so I want to disply : my link name 500
500 is the id of my div
updated :
this is my code :
<li class="$nav_child.getTitle()">
<a href="$nav_child.getURL()" class="$nav_child_class" $nav_child.getTarget()>
$nav_child.getName() <div id="$nav_child.getLayout().getPlid()"></div>
</a>
</li>
for example using this code it displays :
my link name
500
You should use a span instead of the div.
this is one of the differences between span and div.. Div displays text on the next line while span does not.
Make div as "display: inline-block". Your problem will be solved.

How to find a group of classes containing specific text using Selenium IDE

I am trying to test that the correct title, summary, and link appear in search results. For instance, in the example below, I want to confirm that at least one of the records contains the title "Title for Beta," the summary containing the text "Summary for Beta," and a link called "Link."
<ul>
<li class="results">
<h2 class="title">Title for Alpha</h2>
<div class="summary">Summary for Alpha...</li>
<div class="link">Link
</li>
<li class="results">
<h2 class="title">Title for Beta</h2>
<div class="summary">Summary for Beta...</li>
<div class="link">Link
</li>
</ul>
There are many ways to select them. One is using the CSS Class.
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.get("...");
List<WebElement> titles = driver.findElements(By.className("title"));
List<WebElement> summarys = driver.findElements(By.className("summary"));
List<WebElement> links = driver.findElements(By.className("link"));
for (WebElement webElement : titles) {
String innerText = webElement.getText();
// do your test....
}
If your page structure is more complicated you can also use XPath to do that.
If you are new to Selenium, you should have a look to the PageFactory Pattern. This is a nice way to write a much cleaner code.
In the unusual way of selecting Elements by their InnerText you can use XPath.
This XPath selects all elements containing "Title for Alpha" as InnerText
List<WebElement> titles = driver.findElements(By.xpath("//*[contains(text(), 'Title for Alpha')]"));

CSS form[action] not working with Query Strings

Hi all having a few problems with my CSS
I am trying to highlight a link on the navigation based on the page the user is on.
I have this style which works as I would like it to do, but when I pass a query-string into pcisHPprofile.aspx the CSS is not working. Does anyone know how i can get this style to work with query-strings?
body form[action="pcisHPprofile.aspx"] #btnuser
{
padding: 18px 4px 5px 4px;
background-image: url(../images/tabbluleft.gif) ;
background-repeat:no-repeat;
color: #fff;
}
<div id="nav" class="nav" >
<ul>
<li id="tab1">
<a id="btnsession" href="pcissessionlist.aspx" > <span >Session</span></a>
</li>
<li id="tab2">
<a id="btnsystem" href="pcissystemsettings.aspx" > <span >System Settings</span></a>
</li>
<li id="tab3">
</li>
<li id="tab4">
<a id="btnuser" href="pcisuserlist.aspx" > <span >User Logins</span></a>
</li>
<li id="tab5">
<a id="btninterpreter" href="pcisinterpreterlist.aspx" > <span >Interpreter Profile</span></a>
</li>
<li id="tab6"><asp:LinkButton ID="btnreports" runat="server" Visible="false" cssid="cssreports" PostBackUrl="#"><span>Reports</span></asp:LinkButton></li>
</ul>
</div>
I assume that the #btnuser ARE some buttonS inside the some forms, where one of the forms have action="pcisHPprofile.aspx"?
If that is correct, then your error is the fact that you have many buttons with the same id attirbute id="btnuser". The ID attibute MUST be uniqe on the page. change the id="btnuser" to class="btnuser" on the buttons and your selector from:
body form[action="pcisHPprofile.aspx"] #btnuser {
}
to
body form[action="pcisHPprofile.aspx"] .btnuser {
}
Then it should work.
In the first form it might work only if the FIRST button with id="btnuser" is actually inside the form with action="pcisHPprofile.aspx". If it is inside any other form, then it will not work.
Best regards,
SWilk
UPDATE:
After OP updated the question, I think that this form of selector should work:
body form[action^="pcisHPprofile.aspx"] #btnuser {
...
}
It would a element with id=btnuser inside a form, which action begins with "pcisHPprofile.aspx". It would not matter if the acutal action attibute contain only "pcisHPprofile.aspx" or "pcisHPprofile.aspx?any-parameters&and=some-values".
Best regards,
SWilk
Using action attribute for styling is a bad practice. Just give your form a name, class or ID like <form class="pcisHPprofile" action="pcisHPprofile.aspx"> and then apply CSS style to that name/class/id.
form.pcisHPprofile {
padding: ...
Found a working solution. a little hacky which I am not 100% keen on but it works.
I changed the query string to look like this
profile.aspx?-&username=
from this
profile.aspx?username=
and I changed the style to
Form[action|="pcisinterpreterprofile.aspx?"] #tab5
{
background-image: url(../images/tabbluright.gif);
background-repeat:no-repeat;
}
The difference is that I have changed the = to an |=.
[att|=val]
Represents an element with the att attribute, its value either being
exactly "val" or beginning with "val"
immediately followed by "-" (U+002D).
This is primarily intended to allow
language subcode matches (e.g., the
hreflang attribute on the a element in
HTML) as described in RFC 3066
([RFC3066]) or its successor. For lang
(or xml:lang) language subcode
matching, please see the :lang
pseudo-class.
by doing this the css selector works on pages with query strings. you just need to make sure the query string has at least ?- at the start. As I said at the last its not great but works.

using xpath or css : How to get something which is selected on page

using xpath or css : How to get something which is selected on page using following html code.
in following code, Element1 is selected on page and I wanted to find name of element that is selected on page.
<div id="idc" class="tre">
<ul id="idCatT_srt_ul" class="abc">
<li class="treN treB treSelected" title="Element1 title">
<span class="spclass">Element1</span>
</li>
</ul>
</div>
Guessing that you're looking for the <li> that contains selected you can do that with Xpath or CSS selectors like this:
XPath: (if I remember right...)
//*[contains(#class,"selected")]
CSS Selectors:
.treSelected

Resources