简体   繁体   English

无法使用Selenium WebDriver单击跨度

[英]Not able to click on span using selenium webdriver

I have following code in which I want to click on span, 我有以下代码,我想在其中单击跨度,

<div class = "class1">
    <button class="class2 class3 class4 class5" role="button" type="Button">
        <span class="class6">Add</span>
    </button>
</div>

What should be the correct code to click on Add button? 单击添加按钮,正确的代码应该是什么?

I am using Firefox driver. 我正在使用Firefox驱动程序。

Thanks in advance. 提前致谢。

You don't need to click on the span element. 您无需单击span元素。 A span element is not clickable. 跨度元素不可单击。 However, it looks like you can click it because it is inside a button. 但是,您似乎可以单击它,因为它位于按钮内。

All you need to do is locate the button element instead of the span and click on this. 您需要做的就是找到按钮元素而不是跨度,然后单击它。

Use an Xpath as below:- 使用如下所示的Xpath:

//div[@class='class1']//span[@class='class6']

Try code as below:- 尝试如下代码:

driver.FindElement(By.XPath("//div[@class='class1']//span[@class='class6']")).Click();

Hope it will help you :) 希望它能对您有所帮助:)

As has commented, you don't need to click in span tag, but only in the buton. 如前所述,您无需单击span标签,而只需单击按钮。 Some simple selector like this can help you: 这样一些简单的选择器可以帮助您:

driver.FindElement(By.CssSelector("div.class1 > button.class2")).Click();

You can add an action using the onclick handler (though I'd recommend putting it on the button): 您可以使用onclick处理程序添加操作(尽管我建议将其放在按钮上):

<div class = "class1">
    <button onclick='alert("hello world")' class="class2 class3 class4 class5" role="button" type="Button">
        <span class="class6">Add</span>
    </button>
</div>

You can add whatever JavaScript you want in the onclick handler. 您可以在onclick处理程序中添加所需的任何JavaScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM