为 Selenium Webdriver 的以下 href 创建 Xpath

Create the Xpath for the following href for Selenium Webdriver

请帮助我为以下 href 创建 Xpath。我正在使用 Selenium webdriver 并且只有 IE 浏览器。以下是 HTML: 需要 Xpath 用于:a href="javascript:OnEventQueue();">。 我只能用IE浏览器,需要点这个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>

XPath 不是唯一的。不同等级的"precision",可以有不同的路径

例如:

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

使用以下算法 select 您的元素。

CSS

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

Xpath

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

xpath有点丑,肯定不对,但是CSS算法是对的

如果您想匹配任何 属性,包括您想要的 href,请使用此算法。

问自己这些问题:

  1. 我select正在使用的元素是什么?
  2. 我正在 select 的属性是什么?
  3. 属性是否唯一?

在你的例子中,#1 的答案是 <a>。 #2 的答案是 href。答案 #3,可能不是,因为可能有更多 href 这样的。而 ID 通常是唯一的。 (毕竟它被称为 identifier)。您可以做的是查看父项并使用它的 ID。

在CSS中:span#idSpanActionLinks > a 会工作得很好。 (*# 只是 id see here 的 css 快捷方式)

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

编辑

在您发表评论后,由于您想要匹配 href,现在开始:

CSS

a[href*='OnEventQueue']

Xpath

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

两者都可以。

请注意,这两个语句在英语中的意思是:

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