如何单击基于 href 文本的元素



使用C#,我试图单击具有以下文本的元素:

<a target="_blank" href="http://client.mysite.com/nimda/settings.php?campaign=level+1;uid=vmrgssaxs8wkb45oy77pl5xuu&amp;id=172806#rotate"><img alt="Click to edit the rotator settings" title="Click to edit the rotator settings" src="../gfx/icons/small/rotation.png"></a>

所以我尝试使用这个表达式:

driver.FindElement(By.XPath("(//a[contains(@href, 'level 1')]")).Click();

但是我得到无效的表达,为什么?

请注意,我想使用 xpath,因为页面中有很多 a 元素,除了 href 值之外,所有元素都具有相同的属性

括号有问题

driver.FindElement(By.XPath("//a[contains(@href, 'level 1')]")).Click();

顺便说一下,您可以使用CssSelector通过href属性找到元素

driver.FindElement(By.CssSelector("[href*='level 1']")).Click();

最新更新