简体   繁体   中英

Create the Xpath for the following href for Selenium Webdriver

Please help me to create the Xpath for the following href. I am using Selenium webdriver and have only IE browser. Following is the HTML: Need Xpath for : a href="javascript:OnEventQueue();">. I can use only IE browser and need to click on this link.

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML Transitional//EN"><META http-equiv="Content-Type" content="text/html; charset=windows-1252">

    <html>
    <body>
      <form name="runevent1" action="/epace/epace-cgi/eglcgids.exe?sessionid=" method="post" btnSubmit="javascript:preSumbit()" EParams="http://vaim1-apt-120/epace/epace-cgi/eglcgids.exe?sessionid=&Action=runevent1&EventType=&EventInst=#" EParamOptions="http://vaim1-apt-120/epace/epace-cgi/eglcgids.exe?sessionid=&Action=runevent1&EventType=&EventInst=#">
        <table width="100%" align="center" border="0">
          <tbody>
            <tr class="topMenu">
              <td align="right">
                <span id="idSpanActionLinks">
                  <a href="javascript:OnEventQueue();">
                    <font class="topMenu">Event Queue</font>
                  </a>
                </span>
              </td>
            </tr>
          </tbody>
        </table>
      </form>
    </body>
  </html>

Use the following algorithms to select your elements.

CSS

element[attribute(^|*|$|~)='attribute value']

Xpath

//element[(@attribute|contains|starts-with|ends-with(@attribute, 'value'))='value'

The xpath is a little bit uglier, and definitely not correct, but the CSS algorithm is correct.

When you want to match on ANY property, including href like you want to, use this algorithm.

Ask yourself these questions:

  1. what is the element I am selecting?
  2. what is the attribute I am selecting?
  3. is the attribute unique?

In your case, the answer to #1 is an <a> . Answer to number #2 is href . Answer #3, is probably not because there could be more href 's like this. whereas ID's, are usually unique. (it is called an identifier after all). what you can do is look at the parent and use it's ID.

In CSS : span#idSpanActionLinks > a will work just fine. (* # is just a css shortcut for id see here )

In Xpath : //span[@id='idSpaceActionLinks']/a

EDIT

After your comment, since you are wanting to match href , here you go:

CSS

a[href*='OnEventQueue']

Xpath

//a[contains(@href, 'OnEventQueue')]

Both will work.

Note that both of these statements mean, in English:

find me an <a> element, whose attribute href contains the value OnEventQueue

The XPath is not unique. For different levels of "precision", you can have different paths.

Eg:

//*[@id="idSpanActionLinks"]/a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM