跨越使用selenium webdriver无法点击的按钮标签?

时间:2018-01-16 09:23:34

标签: selenium selenium-chromedriver

我正在尝试使用selenium webdriver点击“添加课程”标记按钮,但这对我来说无效。

以下是我从chrome开发人员工具中获取的代码段:

<button type="button" class="btn btn-green" onclick="javascript:AddCourse();">
    <span class="glyphicon glyphicon-plus-sign">
          ::before
    </span> 
    <span translate="portallang_addCourse" class="open-sans ng-scope">
          "Add Course"
    </span>
</button>

2 个答案:

答案 0 :(得分:2)

您需要以下xpath之一:

首选:

//span[contains(text(), 'Add Course')]

这两个,只有总共有2个跨度且第二个跨度总是添加课程:

(//button[@class='btn btn-green']/span)[2]
//button[@class='btn btn-green']/span[2]

扫描整个文档时选项最慢。添加课程只能在页面上出现一次:

//*[contains(text(), 'Add Course')]

答案 1 :(得分:1)

你得到什么错误?您使用什么属性来单击span元素?

你必须使用xpath来表示这些元素。

使用Chrome的 xPath Finder插件并提取此元素的唯一xpath: https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en

您可以随时手动编写唯一的xpath,但这样可以节省时间并且准确无误。

希望这有帮助!