无法使用xpath找到链接

时间:2014-05-23 19:55:37

标签: c# xpath selenium-webdriver

我无法使用xpath在页面上找到链接。当我使用Inspect Element命令时,对象的页面源如下:

<a id="TreeView1_LinkButtonMore" href="javascript:__doPostBack('TreeView1$LinkButtonMore','')">
    See more
</a>

我的Selenium代码如下:

driver.FindElement(By.XPath("//*[@id='TreeView1_LinkButtonMore')")).Click();

我得到的错误如下:

给定的选择器//*[@id='TreeView1_LinkButtonMore')无效或不会产生WebElement。发生以下错误:

InvalidSelectorError: Unable to locate an element with the xpath expression //*[@id='TreeView1_LinkButtonMore')

先谢谢

2 个答案:

答案 0 :(得分:0)

试试这个:

driver.FindElement(By.XPath("//*[@id='TreeView1_LinkButtonMore']")).Click();

然而,值得指出的是,这个完全相同的事情的捷径是:

driver.FindElement(By.Id("TreeView1_LinkButtonMore")).Click();

答案 1 :(得分:0)

您的xpath中存在语法错误。结束应为方括号而不是曲线括号 语法:// tag-name [@ attribute ='value']

尝试一下:

driver.FindElement(By.XPath(“ // a [@ id ='TreeView1_LinkBut​​tonMore']”)))。Click();

相关问题