How do you select a nested css element using Selenium Webdriver? - html

For the following html code:
<div class="r signbox">
<form method="POST" id="loginByEmail">
<dl>
<dt>Seller Login</dt>
<dd>
<input type="text" class="text" name="email" placeholder="Email Address" value="">
<div class="error"></div>
How could I select the "email" attribute in name? The reason why is because the site uses two form boxes that have the same name so I want to target specifically the one in the class "r signbox". Thanks for your help, completely new to Selenium Webdriver.

First select all the content of the class "r signbox" in your selector.
After you pick the desired information, you can take the derised information using a new selector in sequense in the same code block. As you asked, using attr name will be:
$(".r.signbox [class='email']");

By same name you mean the id attribute?
If so, that's odd.
If not, I suggest you to map first the form element and after the input. Doing so, you will be preventing your test to break in case another form to be added between the div.r.signbox and the form.#loginByEmail
Anyway, #Striter awnser is incorrect because he looking for anything with email as class.
A tip is to use the tags names to be more precise:
div.r.signbox input[name='email']
This looks for a div with both r and signbox and inside this element (see the space?) for an input with an attribute name valued as email.
Even if someone put a div with an attribute name valued as email between dd and dt, you locator will work fine.
Here is a good reference to CSS Selectors: https://saucelabs.com/selenium/css-selectors
If you like books, the first chapter of Selenium Testing Tools Cookbook will give you good knowledge: http://www.amazon.com/Selenium-Testing-Cookbook-Gundecha-Unmesh/dp/1849515743

Related

Select multiple but not all (e.g. //*) tag names via XPath?

I am running automation on a webpage that has a lot of elements in the form:
<label ...>"label name"</label>
<div ...></div>
And I need to click the <div> element. I have a function that locates it using the xpath:
driver.findElement(By.xpath("//label[contains(text(),'value')]/following-sibling::div"))
However, some of the element have a slight different form. E.g., one is in the form:
<label ...>"label name"</label>
<br ...></br>
<input ...></input>
And I need to click the <input> element. I can't just use /following-sibling::* because the <br> element is the following sibling.
I could easily just write another function using <input> but I would prefer to just update and reuse the function I already have (and I'm curious). Is there any way to specify an element can have one of multiple tag names?
Maybe something like: /following-sibling::[div or input]
This XPath,
//label[.='l1']/following-sibling::*[self::div or self::input]
will select all div or input sibling elements following a label element with a string value of l1.

Subscript into Placeholder of Input HTML Tag

Hi I'm triyng to use subscript characteres into the placeholder of the html input tag.
<input type="text" class="form-control" id="fi2" placeholder="Installed Power kW<sub>p</sub>">
But i got this: Installed Power kW<sub>p</sub>
If I use for example a supercript (sup) everyhing works well...
Any help?
Best regards,
Daniel
http://www.fileformat.info/info/unicode/char/209a/index.htm
you need some font linked in the webpage that contains subscript p (the fonts are listed on the website cited up), and after, write:
<input type="text" class="form-control" id="fi2">
<script>
var placeholderText = 'Installed Power kW\u209A';
$("#fi2").attr('placeholder', placeholderText);
</script>
That's not possible. HTML attributes cannot have HTML markup inside them.
You'd have to use a <label> instead.
And FYI, here's the MDN site says about the use of placeholder:
Do not use the placeholder attribute instead of a element. Their purposes are different: the attribute describes the role of the form element; that is, it indicates what kind of information is expected, the placeholder attribute is a hint about the format the content should take. There are cases in which the placeholder attribute is never displayed to the user, so the form must be understandable without it.
<input type=“text” class=“form-control” id=“fi2” placeholder=“Installed Power KWₚ”>
I suggest just copy/pasting a subscript char. It sure isn’t a long-term or efficient answer, but it should be adequate for your situation.

Semantically appropriate input title and field tag

I have a form that lists the name of the input, the input, and possibly some help text. It is generally going to appear to be tabular, but I don't want to use a table or div because I imagine there is a more semantically-appropriate option by now.
Here is what it would look like without styles:
Name: <name input>
Address: <address input>
<address help info>
Phone: <phone input>
Notice how it all lines up like a table.
Any ideas for the most appropriate HTML for this?
Why is a table not semantically-appropriate if the data will appear tabular? Use a table, problem, solved.
Use dl. It got redefined in HTML5 so that it’s a "description list" instead of a definition list.
<dl>
<dt>Name:</dt>
<dd>…</dd>
<dt>Address:</dt>
<dd>…</dd>
<dt>Phone:</dt>
<dd>…</dd>
</dl>
If it’s a form with input, don’t forget to use label.
Note that it’s not "required" to use an additional structure, so a form like this is totally fine without dl/table:
<label for="form-name">Name:</label>
<input type="text" id="form-name" name="form-name">
<label for="form-address">Address:</label>
<textarea id="form-address" name="form-address"></textarea>
<label for="form-tel">Telephone:</label>
<input type="tel" id="form-tel" name="form-tel">
A real good question for which it's hard to find one correct answer. According to what I've found, either a table and div would be OK. Even though I would agree neither one of them feels like a real good solution.
Tables:
At http://www.w3.org/TR/html5/tabular-data.html#the-table-element it says:
Tables should not be used as layout aids.
...and...
[...] tables in HTML as a way to control their page layout making it difficult to extract tabular data from such documents.
But you're not really using tables for layout purposes here. You would be using it to present the form input data in a readable manner. As stated in the following sentence (also found in the table spec):
The table element represents data with more than one dimension, in the form of a table.
To me, that means it should be OK to use tables in this case.
Divs
Regarding the div the W3 spec says:
Authors are strongly encouraged to view the div element as an element of last resort, for when no other element is suitable.
But when reading the section spec it says this about the div:
When an element is needed only for styling purposes or as a convenience for scripting, authors are encouraged to use the div element instead.
To me, that means that you can also choose to use div for styling or layout purposes. I've tried to put togehter some thoughts about the use of div in this blog post: http://htmlusedtobeeasy.wordpress.com/2013/07/01/divisions-and-sections/
Sorry for the long answer (my first), hope it helps!

Finding XPath for text in div following input

I got an issue reading XPath. Need some help/advise from experts.
Part of my HTML is below:
<div class = "input required_field">
<div class="rounded_corner_error">
<input id="FnameInput" class="ideField" type="text" value="" name="first_name>
<div class ="help-tooltip">LOGIN BACK TO MAIN</div>
<div class="error-tooltip">
I need to find the XPath of the text message (LOGIN BACK TO MAIN)
Using Firebug I find the XPath
("//html/body/div/div[5]/div/div/form/fieldset/div/div[2]/div[2]/div/div");
But using above XPath I can read only class = help-tooltip but I need to read LOGIN BACK TO MAIN.
Try adding /text() on the end of the xpath you have.
It does not really look like your XPath matches your XHTML element.
You should try something simpler and more generic, such as:
//div[#class="help-tooltip"]/text()
See Selecting a css class with xpath.
I would use:
# Selecting the div element
//input[#id="FnameInput"]/following-sibling::div[#class="help-tooltip"]
# Selecting the text content of the div
//input[#id="FnameInput"]/following-sibling::div[#class="help-tooltip"]/text()
…since a syntactically-valid HTML document will have a unique id attribute, and as such that's a pretty strong anchor point.
Note that the latter expression will select the text node, not the text string content of that node; you need to extract the value of the text node if you want the string. How you do that depends on what tools you are using:
In JavaScript/DOM that would be the .nodeValue property of the text node.
For Nokogiri that would be the .content method.
…but I have no idea what technology you are using your XPath with.

Difference between id and name attributes in HTML

What is the difference between the id and name attributes? They both seem to serve the same purpose of providing an identifier.
I would like to know (specifically with regards to HTML forms) whether or not using both is necessary or encouraged for any reasons.
The name attribute is used when sending data in a form submission. Different controls respond differently. For example, you may have several radio buttons with different id attributes, but the same name. When submitted, there is just the one value in the response - the radio button you selected.
Of course, there's more to it than that, but it will definitely get you thinking in the right direction.
Use name attributes for form controls (such as <input> and <select>), as that's the identifier used in the POST or GET call that happens on form submission.
Use id attributes whenever you need to address a particular HTML element with CSS, JavaScript or a fragment identifier. It's possible to look up elements by name, too, but it's simpler and more reliable to look them up by ID.
Here is a brief summary:
id is used to identify the HTML element through the Document
Object Model (via JavaScript or styled with CSS). id is expected
to be unique within the page.
name corresponds to the form element and identifies what is posted
back to the server.
The way I think about it and use it is simple:
id is used for CSS and JavaScript/jQuery (it has to be unique on a page).
name is used for form handling on the server when a form is submitted via HTML (it has to be unique in a form - to some extent, see Paul's comment below).
See id= vs name=:
What’s the difference? The short answer is, use both and don’t worry about it. But if you want to understand this goofiness, here’s the skinny:
id= is for use as a target like this: <some-element id="XXX"></some-element> for links like this: <a href="#XXX".
name= is also used to label the fields in the message send to a server with an HTTP (HyperText Transfer Protocol) GET or POST when you hit submit in a form.
id= labels the fields for use by JavaScript and Java DOM (Document Object Model).
The names in name= must be unique within a form. The names in id= must be unique within the entire document.
Sometimes the name= and id= names will differ, because the server is expecting the same name from various forms in the same document or various radio buttons in the same form as in the example above. The id= must be unique; the name= must not be.
JavaScript needed unique names, but there were too many documents already out here without unique name= names, so the W3 people invented the id tag that was required to be unique. Unfortunately older browsers did not understand it. So you need both naming schemes in your forms.
Note: attribute "name" for some tags like <a> is not supported in HTML5.
The ID tag - used by CSS, define a unique instance of a div, span or other elements. Appears within the JavaScript DOM model, allowing you to access them with various function calls.
The Name tag for fields - this is unique per form -- unless you are doing an array which you want to pass to PHP/server-side processing. You can access it via JavaScript by name, but I think that it does not appear as a node in the DOM or some restrictions may apply (you cannot use .innerHTML, for example, if I recall correctly).
Generally, it is assumed that name is always superseded by id. This is true, to some extent, but not for form fields and frame names, practically speaking. For example, with form elements, the name attribute is used to determine the name-value pairs to be sent to a server-side program and should not be eliminated. Browsers do not use id in that manner. To be on the safe side, you could use the name and id attributes on form elements. So, we would write the following:
<form id="myForm" name="myForm">
<input type="text" id="userName" name="userName" />
</form>
To ensure compatibility, having matching name and id attribute values when both are defined is a good idea. However, be careful—some tags, particularly radio buttons, must have nonunique name values, but require unique id values.
Once again, this should reference that id is not simply a replacement for name; they are different in purpose. Furthermore, do not discount the old-style approach, a deep look at modern libraries shows such syntax style used for performance and ease purposes at times. Your goal should always be in favor of compatibility.
Now in most elements, the name attribute has been deprecated in favor of the more ubiquitous id attribute. However, in some cases, particularly form fields (<button>, <input>, <select>, and <textarea>), the name attribute lives on, because it continues to be required to set the name-value pair for form submission. Also, we find that some elements, notably frames and links, may continue to use the name attribute, because it is often useful for retrieving these elements by name.
There is a clear distinction between id and name. Very often when name continues on, we can set the values the same. However, id must be unique, and name in some cases shouldn’t—think radio buttons. Sadly, the uniqueness of id values, while caught by markup validation, is not as consistent as it should be. CSS implementation in browsers will style objects that share an id value; thus, we may not catch markup or style errors that could affect our JavaScript until runtime.
This is taken from the book JavaScript - The Complete Reference by Thomas-Powell.
<form action="demo_form.asp">
<label for="male">Male</label>
<input type="radio" name="sex" id="male" value="male"><br>
<label for="female">Female</label>
<input type="radio" name="sex" id="female" value="female"><br>
<input type="submit" value="Submit">
</form>
The forum thread below has answers to the same basic question, but basically, id is used for scripting identification and name is for server-side.
id vs. name attribute for HTML controls
name is deprecated for link targets, and invalid in HTML5. It no longer works at least in the latest Firefox (v13). Change <a name="hello"> to <a id="hello">.
The target does not need to be an <a> tag. It can be <p id="hello"> or <h2 id="hello">, etc. which is often cleaner code.
As other posts say clearly, name is still used (needed) in forms. It is also still used in META tags.
name vs. id
name
Name of the element. For example used by the server to identify the
fields in form submits.
Supporting elements are <button>, <form>, <fieldset>, <iframe>,
<input>, <keygen>, <object>, <output>, <select>, <textarea>, <map>,
<meta>, and <param>
Name does not have to be unique.
id
Often used with CSS to style a specific element. The value of this
attribute must be unique.
Id is a global attribute. Global attributes can be used on all elements, though the attributes may have no effect on some elements.
Must be unique in the whole document.
This attribute's value must not contain white spaces, in contrast to
the class attribute, which allows space-separated values.
Using characters except ASCII letters and digits, '_', '-' and '.'
may cause compatibility problems, as they weren't allowed in HTML 4.
Though this restriction has been lifted in HTML 5, an ID should start
with a letter for compatibility.
<body>
<form action="">
<label for="username">Username</label>
<input type="text" id="username" name="username">
<button>Submit!</button>
</form>
</body>
As we can see here, "id" and "for" elements are interconnected. If you click on the label (Username) then the input field will be highlighted (this is useful for mobile users and is considered as a good practice).
On the other hand, the "name" element is useful while submitting the form. Whatever you enter in the input field it will be displayed on the URL. Please see the attached image.
The ID of a form input element has nothing to do with the data contained within the element. IDs are for hooking the element with JavaScript and CSS. The name attribute, however, is used in the HTTP request sent by your browser to the server as a variable name associated with the data contained in the value attribute.
For instance:
<form>
<input type="text" name="user" value="bob">
<input type="password" name="password" value="abcd1234">
</form>
When the form is submitted, the form data will be included in the HTTP header like this:
If you add an ID attribute, it will not change anything in the HTTP header. It will just make it easier to hook it with CSS and JavaScript.
ID is used to uniquely identify an element.
Name is used in forms. Although you submit a form, if you don’t give any name, nothing will will be submitted. Hence form elements need a name to get identified by form methods like "get or push".
And only the ones with the name attribute will go out.
If you're not using the form's own submit method to send information to a server (and are instead doing it using JavaScript) you can use the name attribute to attach extra information to an input - rather like pairing it with a hidden input value, but it looks neater because it's incorporated into the input.
This bit does still currently work in Firefox although I suppose in the future it might not get allowed through.
You can have multiple input fields with the same name value, as long as you aren't planning to submit the old fashioned way.
Id:
It is used to identify the HTML element through the Document Object Model (DOM) (via JavaScript or styled with CSS).
Id is expected to be unique within the page.
Name corresponds to the form element and identifies what is posted back to the server.
Example:
<form action="action_page.php" id="Myform">
First name: <input type="text" name="FirstName"><br>
<input type="submit" value="Submit">
</form>
<p>The "Last name" field below is outside the form element, but still part of the form.</p>
Last name: <input type="text" name="LastName" form="Myform">
In all the time this question has been around, I am chagrined (and perhaps a bit saddened) that nobody has thought to mention accessibility which, though always important, has been steadily gaining support amongst both management and software engineers (just from my personal observations; no hard data to back that up).
One statistic I can provide is this (source):
So awareness of accessibility shortcomings show a steadily growing trend. The same reference mentions that, from those numbers, one can observe that at least one lawsuit is filed every hour!
So how does accessibility weigh in on name vs id?
According to the World Wide Web Consortium (W3C):
The for attribute of the label must exactly match the id of the
form control.
Based on personal experiences and according to the W3Schools description for attributes:
ID is a global attribute and applies to virtually all elements in HTML. It is used to uniquely identify elements on the Web page, and its value is mostly accessed from the frontend (typically through JavaScript or jQuery).
name is an attribute that is useful to specific elements (such as form elements, etc.) in HTML. Its value is mostly sent to the backend for processing.
HTML Attribute Reference
Below is an interesting use of the id attribute. It is used within the <form> tag and used to identify the form for <input> elements outside of the </form> boundaries so that they will be included with the other <input> fields within the form.
<form action="action_page.php" id="form1">
First name: <input type="text" name="fname"><br>
<input type="submit" value="Submit">
</form>
<p>The "Last name" field below is outside the form element, but still part of the form.</p>
Last name: <input type="text" name="lname" form="form1">
There is no literal difference between an id and name.
name is an identifier and is used in the HTTP request sent by the browser to serve as a variable name associated with data contained in the value attribute of the element.
The id on the other hand is a unique identifier for browser, client side and JavaScript. Hence the form needs an id while its elements need a name.
id is more specifically used in adding attributes to unique elements. In DOM methods, Id is used in JavaScript for referencing the specific element you want your action to take place on.
For example:
<html>
<body>
<h1 id="demo"></h1>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
Same can be achieved by name attribute, but it's preferred to use id in a form and name for small form elements like the input tag or select tag.
Both name and id is targetable by # so not sure why ID was mentioned for this task exclusively.
I often Inspect those attributes to create specific links to bookmark (where clicking on header with mouse cursor to do the same is not provided for some reason) such as the Option File Inclusions
section of MySQL 5.6 4.2.2.2 Using Option Files documentation:
https://dev.mysql.com/doc/refman/5.6/en/option-files.html#option-file-inclusions
where it's defined as <a name="option-file-inclusions"></a> (with absolutely no forms involved).
I think, the name attribute is also older than id in HTML.
The id will give an element an id, so once you write real code, (like JavaScript) you can use the id to read elements. The name is just a name, so the user can see the name of the element, I guess.
Example:
<h1 id="heading">text</h1>
<script>
document.getElementById("heading"); // Reads the element that has the id "heading".
</script>
// You can also use something like this:
document.getElementById("heading").value; // Reads the value of the selected element.