how to identify xpath for a link?

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

how to identify xpath for a link?

Eleo-3
Hi All,

I am new to Selenium IDE.
Selenium IDE is not able to capture/record clicking on links.
Its working when i use "link" tag but the links on my website (XHTML) are dynamic.
I tried giving xpath but nothing is working out.
Could you plz help me out...
My page looks like this :

<html>
 <head>
</head>
<body>
    <div></div>
    <div></div>
    <div class="abc"> <a href="/m/url?cd=1&ct=res&ei=S5LNR-CzDovO6gH49dXXAw&oi=blended&q=http%3A%2F%2Fwww.pizzahut.com%2F&sa=X&usg=AFQjCNGw0o5XfH26Iggmnsv4CICHITfIjw">Pizza Hut</a></div>
    <div></div>
</body>
</html>

want to capture the link "Pizza Hut"
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40362#40362

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
There are a few ways to do this.  You might look at identifying the link by the text that is being linked.  So, in your example, something like <span style="font-family: Courier New;">//a/text()[.='Pizza Hut']</span> might work.  The <span style="font-family: Courier New;">text()</span> node in the XPath indicates whatever text is found between the tags of the node's parent.  (Thus, the <span style="font-family: Courier New;">text()</span> here identifies any text within the anchor tags.)

If the text itself is also dynamic, you can always identify an XPath node numerically.  For instance, if I have the following code....

<div style="font-family: Courier New;">
&lt;ul id="list"&gt;
&nbsp;&nbsp;&lt;li id="xyzabc"&gt;123&lt;/li&gt;
&nbsp;&nbsp;&lt;li id="defghi"&gt;456&lt;/li&gt;
&nbsp;&nbsp;&lt;li id="jklmno"&gt;789&lt;/li&gt;
&lt;/ul&gt;
</div>

...(where the <span style="font-family: Courier New;">id</span>s and list item text are dynamically created), I could specify the second list item via the following XPath expression, rather than using an <span style="font-family: Courier New;">id</span> or text: <span style="font-family: Courier New;">//ul[@id='list']/li[2]</span> and have the correct node selected.

You can then use <span style="font-style: italic;">storeText</span>, <span style="font-style: italic;">storeAttribute</span>, etc., to capture whatever you're wanting for that node you've indicated.
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40363#40363

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
Hi
Thanks for the prompt response....
I m still stuck with it....
My file actually looks like this:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<div>
<img width="48" height="17" style="vertical-align: middle;" src="/xhtml/images/logo_small.gif" alt="Google"/>
</div>
<div>
<div class="padbottom">
Results:
<b>
<a href="#query">Pizza</a>
</b>
</div>
<div class="section">
</div>
<div class="padbottom">
<div class="padbottom">
*<a href="/m/url?cd=1&ct=res&ei=w6jNR8CXDYvO6gG4q_7ZAw&oi=blended&q=http%3A%2F%2Fwww.pizzahut.com%2F&sa=X&usg=AFQjCNGkb8zx_Puzpcy1e8VY0fIMzjnCdw">Pizza Hut</a>*
 Online ordering, menu and nutrition information, and locator for stores in
<b>...</b>
<span class="green">www.pizzahut.com/</span>
</div>
<div class="padbottom">
</div>
<div class="padbottom">
</div>
<div class="padbottom">
</div>
<div class="padbottom">
</div>
<div class="padbottom">
</div>
<div class="sftr">
</div>
</div>
<div class="section"> Search nearby </div>
<div class="padbottom">
Enter your location
<br/>
<span class="hint"> (e.g., 10010, new york ny, 100 5th ave new york ny) </span>
<form action="/m/search">
<div>
</div>
</form>
</div>
<div class="section"> More results in </div>
<div class="padbottom">
<div class="li">
</div>
</div>
<div class="sectionsubheading">
</div>
<div>
<form action="/m/search">
<div>
</div>
</form>
</div>
</div>
<div class="small padtop">
</div>
<div class="center small padtop">
</div>
</body>
</html>

and i am using the xpath as : xpath=/html/body/div[2]/div[3]/div[1]/a      is this correct? for the link in bolds
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40367#40367

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
You should try to use a cleaner xpath query. (ie, one that won't break if the structure of your page changes lightly..)

for example : xpath=//a[text()="Pizza Hut"] or xpath=//a[@href="http://yoururl"]
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40368#40368

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
In reply to this post by Eleo-3
There are two problems here I can see right off.  First, you need your XPath query in quotation marks (so <span style="font-family: Courier New;">xpath=<span style="font-weight: bold;">"</span>/html/body/div[2]/div[3]/div[1]/a<span style="font-weight: bold;">"</span></span>) if you're using it like you are.  Second, like Peter said, try a cleaner query if possible.  For instance, <span style="font-family: Courier New;">//</span> would eliminate the need for the <span style="font-family: Courier New;">/html/body</span> nodes.

Also, do you have three levels of <span style="font-family: Courier New;">DIV</span>s that are all dynamic in addition to the anchor tag?  If <span style="font-family: Courier New;">div[2]</span>, <span style="font-family: Courier New;">div[3]</span>, and <span style="font-family: Courier New;">div[1]</span> are not dynamic, you would serve your goal better if you cut that to something like <span style="font-family: Courier New;">//div[@class='padbottom']/a</span>.  (Obviously here, you have more than one <span style="font-family: Courier New;">DIV</span> with that class, so that exact query may not work in this case.)  It <span style="font-weight: bold; font-style: italic;">looks</span> like you could even get away with <span style="font-family: Courier New;">//a/text()[.='Pizza Hut']</span> or <span style="font-family: Courier New;">xpath="//a/text()[.='Pizza Hut']</span>.

Keep in mind that the more specific you are (by having the second <span style="font-family: Courier New;">DIV</span>, followed by the third <span style="font-family: Courier New;">DIV</span> child under that, followed by the first <span style="font-family: Courier New;">DIV</span> child under <span style="font-weight: bold; font-style: italic;">that</span>), the greater the risk that the code will change in the future enough that your query will fail and you'll have to fix it.  But, if you know, for example, that the link will always use the text "Pizza Hut," you could use the <span style="font-family: Courier New;">//a/text()[.='Pizza Hut']</span> query instead.
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40369#40369

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
Hey

When i put xpath="//a/text()[.='Pizza Hut']" I get this error:

[error] Unexpected Exception: code -> 52, INVALID_EXPRESSION_ERR -> 51, TYPE_ERR -> 52, message -> The expression cannot be converted to return the specified type., result -> 2153447476, name -> NS_ERROR_DOM_TYPE_ERR, filename -> chrome://selenium-ide/content/selenium/scripts/selenium-browserbot.js, lineNumber -> 1114, columnNumber -> 0, location -> JS frame :: chrome://selenium-ide/content/selenium/scripts/selenium-browserbot.js :: anonymous :: line 1114, inner -> null, data -> null


and with xpath=//a/text()[.='Pizza Hut']
i get this:

[error] Element xpath=//a/text()[.='Pizza Hut'] not found
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40424#40424

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
that's a wrong xpath, you put text() outside the brackets. the correct one is as peterbernier says: xpath=//a[text()="Pizza Hut"], you can make it short as xpath=//a[.='Pizza Hut'].
maybe the link is created dynamically, that's why you got error of "element not found". you should wait for the link present first, then click it. the code snippet is like:

for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (selenium.isElementPresent("xpath=//a[.='Pizza Hut']")) break; } catch (Exception e) {}
        Thread.sleep(1000);
}
selenium.click("xpath=//a[.='Pizza Hut']");
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40442#40442

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
Sorry about that.  What I put before <span style="font-weight: bold; font-style: italic;">should</span> work (I use it in my own IDE code, for instance, without any errors), but what <a href="http://forums.openqa.org/profile.jspa?userID=4520" title="gim_h">gim_h</a> describes is briefer, for sure.  For instance, if you go to <a href="http://www.computerpoweruser.com/editorial/article.asp?article=articles%2Farchive%2Fc0212%2F49c12%2F49c12.asp" target="_blank">this link</a>, you can find <span style="font-family: Courier New;">/main/a/text()[2]</span> on the page, which would indicate that <span style="font-family: Courier New;">text()</span> is just like any other node.  As such, you can add a "self equals" descriptor.  Not sure why that wouldn't have worked, but <a href="http://forums.openqa.org/profile.jspa?userID=4520" title="gim_h">gim_h</a>'s example is briefer anyhow, so use that instead.
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40484#40484

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
Hi All,

Thanks I really appreciate ur inputs and helping me to learn selenium
But i m still stuck with it, evn a very simple xpath is not working,
can it be some other problem apart frm syntax???
As I am able to locate texts for google home page but not for my page, don't know the reason.
My File is an XHTML file.

Regards,
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40487#40487

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
In reply to this post by Eleo-3
Hi I have one similar kind of problem. In my source
<td class="uta"></td>
       <td id="chris1utaname" class="uta">
             <a href="element/node.jsp?node=3&utaName=chris1">
           chris1
             </a>
So I used your method $sel->click("xpath=//a[.='chris1']");      but I still got error like ERROR: Element xpath=//a[.='chris1'] not found
Anybody please help me on this.
-Ka
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40501#40501

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
use xpath viewer add on for mozilla
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40811#40811

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
Hi Rajat,

Thanks for your reply.When i installed xpath.I got this path id('chris1utaname')/a so how can I used it Can you explain me  in more  detail.
-kartik
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40836#40836

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
In reply to this post by Eleo-3
Hi, all!
I catch xpath of my link by 'XPath Checker' addon for Mozilla.
I got this: id('_id46:objectList:com.merimetr.voland.entities.PantherConfiguration@12088db:_id84').
The part '12088db' generated dynamic, but all the rest is constant.
Please help me: how I can write correctly xpath for this link?
May be any mask or regular expression???
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40848#40848

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

Eleo-3
In reply to this post by Eleo-3
Use the Firebug addon on firefox so that you can identify the correct xpath for the element. Dont forget to add '/' at the start of the xpath that you will be getting. Hope this helps


https://addons.mozilla.org/en-US/firefox/addon/1843
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40852#40852

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

kartik_bhatt
In reply to this post by Eleo-3
Hi Paul Grandjean,
Can you please write this script in perl becasue I am writing selenium test using perl.
-ka

 
Paul Grandjean wrote
that's a wrong xpath, you put text() outside the brackets. the correct one is as peterbernier says: xpath=//a[text()="Pizza Hut"], you can make it short as xpath=//a[.='Pizza Hut'].
maybe the link is created dynamically, that's why you got error of "element not found". you should wait for the link present first, then click it. the code snippet is like:

for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (selenium.isElementPresent("xpath=//a[.='Pizza Hut']")) break; } catch (Exception e) {}
        Thread.sleep(1000);
}
selenium.click("xpath=//a[.='Pizza Hut']");
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40442#40442

---------------------------------------------------------------------
To unsubscribe, e-mail: selenium-ide-users-unsubscribe@openqa.org
For additional commands, e-mail: selenium-ide-users-help@openqa.org
Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

kartik_bhatt
In reply to this post by Eleo-3
Hi Paul Grandjean,

I wrote similar in perl something like this

for($second=0 ;; $second++)
{
  if($second >=60)
      {  
        $sel->set_timeout(3000000);
      }
        try
        {
            $sel->is_element_present("xpath=//a[.='chris1']");
            break;
        }
       
          catch (Exception e) {}
               Thread.sleep(1000);
  }  
        $sel->click("xpath=//a[.='chris1']");
I checked thi sone put it is not working properly.
-kartik
--------------

Eugene-57 wrote
that's a wrong xpath, you put text() outside the brackets. the correct one is as peterbernier says: xpath=//a[text()="Pizza Hut"], you can make it short as xpath=//a[.='Pizza Hut'].
maybe the link is created dynamically, that's why you got error of "element not found". you should wait for the link present first, then click it. the code snippet is like:

for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (selenium.isElementPresent("xpath=//a[.='Pizza Hut']")) break; } catch (Exception e) {}
        Thread.sleep(1000);
}
selenium.click("xpath=//a[.='Pizza Hut']");
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40442#40442

---------------------------------------------------------------------
To unsubscribe, e-mail: selenium-ide-users-unsubscribe@openqa.org
For additional commands, e-mail: selenium-ide-users-help@openqa.org
Reply | Threaded
Open this post in threaded view
|

Re: how to identify xpath for a link?

kartik_bhatt
I got it. thank you guys for your help.
-kartik
kartik_bhatt wrote
Hi Paul Grandjean,

I wrote similar in perl something like this

for($second=0 ;; $second++)
{
  if($second >=60)
      {  
        $sel->set_timeout(3000000);
      }
        try
        {
            $sel->is_element_present("xpath=//a[.='chris1']");
            break;
        }
       
          catch (Exception e) {}
               Thread.sleep(1000);
  }  
        $sel->click("xpath=//a[.='chris1']");
I checked thi sone put it is not working properly.
-kartik
--------------

Eugene-57 wrote
that's a wrong xpath, you put text() outside the brackets. the correct one is as peterbernier says: xpath=//a[text()="Pizza Hut"], you can make it short as xpath=//a[.='Pizza Hut'].
maybe the link is created dynamically, that's why you got error of "element not found". you should wait for the link present first, then click it. the code snippet is like:

for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (selenium.isElementPresent("xpath=//a[.='Pizza Hut']")) break; } catch (Exception e) {}
        Thread.sleep(1000);
}
selenium.click("xpath=//a[.='Pizza Hut']");
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=12760&messageID=40442#40442

---------------------------------------------------------------------
To unsubscribe, e-mail: selenium-ide-users-unsubscribe@openqa.org
For additional commands, e-mail: selenium-ide-users-help@openqa.org