[Webdriver] How we can handle elements which dont have id , name or any fixed xpath

131 views
Skip to first unread message

Niloy SQA

unread,
Dec 21, 2011, 5:12:42 AM12/21/11
to webdriver
Hi Guys

Currently I am working on a project where for page
elements I dont have any id, name and the xpath is always changing
after reload.

How can i click on that element on these elements ?

Plz have a look on this image:
https://docs.google.com/leaf?id=0B25PDo55xObZYTkzZmUxYzgtMTczYi00MzMzLWEyZDUtZTJiNWMzY2M1N2Ni&hl=en_US

Such an sample I want to click on that folder. How can i
do it ?

Waiting for your suggestion !



Code for that folder:

<div class="entry-wrapper left">
<dl id="/home/trietle" class="entry dir left" data-title="My
Documents" data-selected="false" data-root="1" data-ext="n/a" data-
type="dir" data-dirpath="/home/trietle" data-path="/home/trietle">

// this is the object link
<dt class="thumbnail n/a ext-n/a"></dt>


<dd class="info wrap-file">My Documents</dd>
<input class="hidden input rename" type="text" value="My Documents"
propercase="true" trim="true" dojotype="dijit.form.TextBox"
name="firstname">
<dd class="edit right" _dijitmenumenu="1">&nbsp;</dd>
</dl>

darrell

unread,
Dec 21, 2011, 9:26:46 AM12/21/11
to webdriver
I like to think about how I would find the element as a manual tester.
Can I find it with the same information programmatically? As a tester,
what would the test case be? Do you click on the folder with "My
Documents" under it? Is there only one folder with the label "My
Documents"? If that is the case then the consistent CSS might be:

"dl[data-title=My Documents]>dt"

The other possibility is to use an xpath which finds the DD tag with
text()='My Documents', go up one to the DT, e.g.

""//dd[text()='My Documents']/.."

The key is that the 'user' will be looking for "My Documents" in all
of my examples. So the WebDriver could should be doing the same.


On Dec 21, 5:12 am, Niloy SQA <niloy.c...@gmail.com> wrote:
> Hi Guys
>
>             Currently I am working on a project where for page
> elements I dont have any id, name  and the xpath is always changing
> after reload.
>
>             How can i click on that element on these elements ?
>
>             Plz have a look on this image:https://docs.google.com/leaf?id=0B25PDo55xObZYTkzZmUxYzgtMTczYi00MzMz...

Niloy SQA

unread,
Dec 23, 2011, 2:17:39 AM12/23/11
to webdriver
Hay friend

Thank you for your quick reply. I have tried for your 2nd
way but did not work for me..

I used this one : driver.findElement(By.xpath("//
dd[text()='My Documents']/")).click();

Give me the following error trace:

org.openqa.selenium.InvalidSelectorException: The given
selector //dd[text()='My Documents']/ is either invalid or does not
result in a WebElement. The following error occurred:
[InvalidSelectorError] Unable to locate an element with the xpath
expression //dd[text()='My Documents']/ because of the following
error:
[Exception... "The expression is not a legal expression." code: "51"
nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)"
location: "file:///C:/Users/Niloy/AppData/Local/Temp/
anonymous3572116737383619975webdriver-profile/extensions/
fxdr...@googlecode.com/resource/modules/atoms.js Line: 2362"];
duration or timeout: 18.84 seconds.

Did i missed something ?

Waiting for your feedback.

Regards
Niloy

Niloy SQA

unread,
Dec 23, 2011, 3:46:45 AM12/23/11
to webdriver
Hi Friends

I have tried the all following ways :

driver.findElement(By.xpath("//dd[text()='My
Documents']//")).click();
driver.findElement(By.cssSelector("dl[data-title=My
Documents]>dt")).click();
driver.findElement(By.cssSelector("div:contains('My
Documents')")).click();

but no one did not work for me. Am i doing any
mistake ?

Love to hear your feedback.

Regards


On Dec 23, 2:17 pm, Niloy SQA <niloy.c...@gmail.com> wrote:
> Hay friend
>
>              Thank you for your quick reply. I have tried for your 2nd
> way but did not work for me..
>
>              I used this one :  driver.findElement(By.xpath("//
> dd[text()='My Documents']/")).click();
>
>              Give me the following error trace:
>
>              org.openqa.selenium.InvalidSelectorException: The given
> selector //dd[text()='My Documents']/ is either invalid or does not
> result in a WebElement. The following error occurred:
> [InvalidSelectorError] Unable to locate an element with the xpath
> expression //dd[text()='My Documents']/ because of the following
> error:
> [Exception... "The expression is not a legal expression."  code: "51"
> nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)"
> location: "file:///C:/Users/Niloy/AppData/Local/Temp/
> anonymous3572116737383619975webdriver-profile/extensions/
> fxdri...@googlecode.com/resource/modules/atoms.js Line: 2362"];

Mark Collin

unread,
Dec 23, 2011, 4:19:49 AM12/23/11
to webd...@googlegroups.com
Try changing:

driver.findElement(By.xpath("//dd[text()='My Documents']//")).click();

To:

driver.findElement(By.xpath("//dd[.='My Documents']")).click();

The main change is removing the tailing // in your xpath. Text() is
perfectly fine to use, '.' is just a shortcut that means text() which I
prefer to use to cut down on the size of XPath's.

Hi Friends

Regards

--
You received this message because you are subscribed to the Google Groups
"webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to
webdriver+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/webdriver?hl=en.


--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

Niloy SQA

unread,
Dec 23, 2011, 4:59:13 AM12/23/11
to webdriver
Hay Mark

Awesome !!! It works now. Thank you very much.

I have a couple of question still now.

1. What can I do if there is two folder icon name with
same text "test" and "test" ?

2. If there is no text like "My Documents" then how can
I click on that file icon ?

waiting to hear from you

Thank you once again.
> For more options, visit this group athttp://groups.google.com/group/webdriver?hl=en.
>
> --
> This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.
>
> If you have received this email in error please notify postmas...@ardescosolutions.com

Mark Collin

unread,
Dec 23, 2011, 5:15:43 AM12/23/11
to webd...@googlegroups.com
If you have two elements with the same text you could use an xpath like
this:

(//dd[.='My Documents'])[1]

(//dd[.='My Documents'])[2]

This is brittle however and not really something to do unless you have no
other option.

Generally I would suggest you find an element that has an ID associated with
it and then walk down the tree to the element you are looking for, this
makes your XPath much more reliable and saves having to search the entire
document for the element you want.

Hay Mark

Thank you once again.

--


This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

Niloy SQA

unread,
Dec 23, 2011, 5:34:00 AM12/23/11
to webdriver
hay Mark

Thank you for your suggestion .

My 2nd question was : If there is no text like "My
Documents" then how can I click on that file icon ?


Regards
> If you have received this email in error please notify postmas...@ardescosolutions.com

Mark Collin

unread,
Dec 23, 2011, 5:37:27 AM12/23/11
to webd...@googlegroups.com
And as stated before:

hay Mark


Regards

--


You received this message because you are subscribed to the Google Groups
"webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to
webdriver+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/webdriver?hl=en.

--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

darrell

unread,
Dec 23, 2011, 8:16:42 AM12/23/11
to webdriver
Niloy,

If you were manually testing the application, how would you decide
which folder to click on? In the case of two folders named 'test',
would your test case say, "Click on the first folder named 'test'." Or
would the test case say, "Click on any random folder." The important
thing is knowing what you want to test before you automate it.
Automation is HOW you conduct the test. It does not dictate WHAT you
want to test. In other words, your automation should automate the test
you already have. If you are automating without already having a test
case, you run the risk of automating whatever is easiest to automate
rather than what is important to the customer.

In the example below, if you are testing it manually, how do you
decide the folder with no text is the folder to click on? If it is the
only folder with no text then create a locator which finds the folder
with "" for the text. Or find all folders using findElements() and
iterator over them until you find the folder with no text.

If the test case is pick a random folder, you could find all folders
using findElements(). Get the size of the List, pick a random number
modulo the size of the List then select that folder by index.

It all depends on what you are trying to test. Don't let your
automation dictate your test case. You have to let the test case
dictate the automation.

Darrell

Niloy SQA

unread,
Dec 23, 2011, 10:56:46 AM12/23/11
to webdriver
Hay Darrell

Thank you you very much for sharing your idea. I
really loved to read your comment.

Yes I do have the clear idea about the test cases and
automation concepts. We do have the testcase and then we turn that in
to manual or automate.

On this project. this is a developing project's
backlog and we are developing the test cases. While we are having the
testcases then I am researching as well what kind of automating
techniques I need to gather together to automate them as well.

I have a question for you. In your idea, what is an
efficient tool for generating anything's xpath to use in the
webdriver ?

Regards
Niloy

darrell

unread,
Dec 28, 2011, 10:10:00 AM12/28/11
to webdriver
If you are using Internet Explorer 8 or higher you can press F12 to
open the developer tools. If you are using Firefox you need to install
Firebug then F12 will open Firebug. If you are using Chrome then CTRL-
SHIFT-I will open the developer tools.

Beyond that, the only tool I use is my brain and the W3 standards.

Reading the W3 standards (or any standards documentation, ISO, ANSI,
IEEE, etc.) can be difficult at first. Especially if you have been
learning from books like "Web Design in 21 Days" or "Software Testing
for Dummies." However, the more you read and understand standards
documentation, the easier it gets to read other standards documents.
If generating XPath was easy enough for a piece of software then why
would they pay you to do the work? There are probably a dozen XPath
locators for any given element on a page. Some will work once and need
to be revised on the next release of the application. Some will work
within the design pattern of the application and might never need
updating. There is no way for a piece of software to spot the design
pattern and know which locator will work best. This is what they pay
you to do.

Excessively long XPath is brittle and will need a great deal of
revising from release to release. Extremely short XPath will sometimes
find the wrong element between releases. This leads to a test which
fails unpredictably and can be difficult to debug. Not something you
want in an automation suite. Finding the right balance is your job.
The first time you select a locator it might need revising for the
next release. You need to look at why you selected the first locator
when selecting the revised locator. The second locator should work for
the first release and the second release. When the locator fails, you
need to select a new locator which would have worked on the first
release and all subsequent releases, including the next release. After
a while you should start to see the pattern. The pattern is usually
derived from some design pattern the application is being developed
with. Learn about Design Patterns, it will be extremely helpful in
generating good test automation. If the developers change the tools,
libraries, design patterns, etc. you should expect the locators to
fail. At this point, selecting a locator which works with the next
release but does not work with the previous release makes sense. Major
change in development usually implies major change in test automation.
It would be difficult for a tool to realize when it needs to abandon
old locators.

Essentially, automation is all about finding elements (locators),
performing actions on them, confirming the expected results (usually
involves more locators). Two thirds of the work is about the locators.
Learning XPath, CSS and DOM will make your job that much easier.

XPath 1.0: http://www.w3.org/TR/xpath/
XPath 2.0: http://www.w3.org/TR/xpath20/
XPath functions: http://www.w3.org/TR/xpath-functions/
CSS 1.0: http://www.w3.org/TR/2008/REC-CSS1-20080411/
CSS 2.0: http://www.w3.org/TR/CSS2/
CSS 3.0: http://www.w3.org/TR/selectors/
DOM: http://www.w3.org/TR/DOM-Level-2-HTML/html.html

When possible, use CSS selectors as they are faster. Some things are
easier to locate using XPath and XQuery (XPath functions). It is
better to have a test run slow and be easy to maintain. So if CSS
selectors are complex and unintuitive you might want to use XPath
functions instead.

Darrell


On Dec 23, 10:56 am, Niloy SQA <niloy.c...@gmail.com> wrote:
> Hay Darrell

Niloy SQA

unread,
Dec 28, 2011, 11:18:07 AM12/28/11
to webdriver
Hay Darrell

Thank you for your details information here. This is
really valuable for new automation testers.

For finding xpath i love to use sele IDE, firebug ,
fire pather and some tools which can directly generate the xpath of an
element . But Auto directly generated xpath is not always working
fine.

But my question was - do you have any efficient ,
specific tools for generating xpath and css locator ? If you have
put their name please.


Regards
Niloy
> ...
>
> read more »

darrell

unread,
Dec 29, 2011, 8:33:49 AM12/29/11
to webdriver
Short answer: No.
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages