如何点击没有ID或名称的链接?

时间:2015-09-25 17:53:03

标签: c# selenium selenium-webdriver click angularjs-ng-href

HTML code:

<a ng-repeat="group in groups()" ng-href="phones/Apple/iPhone%203G%20or%203GS/" class="ng-scope" href="phones/Apple/iPhone%203G%20or%203GS/">
        <div class="card-box device-box">
            <span class="card-text ng-binding">iPhone 3G or 3GS</span>
            <i class="fa fa-chevron-right visible-xs"></i>
            <div class="clearfix"></div>
        </div>
    </a>

C#代码:

driver.FindElement(By.LinkText("IPHONE 3G 16GB")).Click();

2 个答案:

答案 0 :(得分:1)

我会在这里创建一个XPath表达式来查找a元素,其中span元素包含iPhone 3G or 3GS文本:

driver.FindElement(By.XPath("//a[div/span = 'iPhone 3G or 3GS']")).Click();

您也可以尝试使用PartialLinkText定位器来接近它:

driver.FindElement(By.PartialLinkText("IPHONE 3G 16GB")).Click();

答案 1 :(得分:0)

尝试使用以下xpath

By.XPath("//a//span[text()='iPhone 3G or 3GS']")
相关问题