如何点击<a class=""> with using xpath

时间:2019-03-26 11:40:46

标签: java html selenium-webdriver

Using xpath, sélénium doesn't recognize the element, what's the correct xpath?

HTML:

<a class="xmi" onclick="this.focus();return false; "href="#">
 <span title="">SITE A</span>
</a>

My XML locator file:

<domElements>
   <name>Site_A</name>
   <locators>
      <type>XPATH</type>
      <value>//span[contains(@title,'SITE A')]</value>
      <priority>0</priority>
   </locators>
      </domElements> 

Java code calling my locator:

this.locate("Site_A").click();

}

}

2 个答案:

答案 0 :(得分:0)

SITE A不是标题(在这种情况下为空字符串),而是元素文本。使用text().定位它

//span[contains(., 'SITE A')]

如果该元素位于<iframe>内,则需要先将其切开

driver.switchTo().frame("afr::PushIframe");

答案 1 :(得分:0)

SITE A不是 value 属性,而是 innerHTML ,因此您需要:

<domElements>
   <name>Site_A</name>
   <locators>
      <type>XPATH</type>
      <value>//span[@title and text()='SITE A']</value>
      <priority>0</priority>
   </locators>
</domElements> 

或者,您也可以更改<value>以将父节点包括为:

<domElements>
   <name>Site_A</name>
   <locators>
      <type>XPATH</type>
      <value>//a[@class='xmi']/span[@title and text()='SITE A']</value>
      <priority>0</priority>
   </locators>
</domElements>