简体   繁体   English

Xpath的FindElement不适用于“添加新”按钮

[英]FindElement by Xpath is not working for “add New” button

<a class="add-new pull-right" href="javascript:void(0);">

    <span class="add-new-icon omedia-plus-circled"></span>

     Add New

</a>

The code is for "Add New" button which is present. 该代码用于显示的“添加新”按钮。

The xpath for the Add New button is : “添加新”按钮的xpath为:

/x:html/x:body/x:nav[2]/x:div/x:div[2]/x:div/x:a

I have tried this code: 我已经试过这段代码:

wd.findElement(By.xpath("//a[contains(@href,\"javascript:void[0]\")]")).click();

but its not working. 但它不起作用。 Can you please help me with this.. 你能帮我这个忙吗..

How about : 怎么样 :

wd.findElement(By.xpath("//a[contains(@href,\"javascript:void(0)\")]")).click();
                                                             ^ ^
                                                             Notice the use of 
                                                             brackets

Why not go with classname? 为什么不使用classname?

wd.findElement(By.className("add-new")).click(); wd.findElement(By.className(“ add-new”))。click();

A thing you should be aware of in general is that xpath expressions are considered the worst way to identify your elements than the use of CSS selectors, especially if you are testing in IE as the xpath implementation there is not native and is very slow. 通常应该注意的一件事是,与使用CSS选择器相比,xpath表达式被认为是识别元素的最糟糕的方法,尤其是如果您正在IE中进行测试,因为xpath实现不是本机的,而且速度很慢。 I suggest reading over http://saucelabs.com/resources/selenium/css-selectors for a brief look at some examples and also maybe have aread of http://saucelabs.com/resources/selenium/selenium-xpath-marks-the-spot in order to see some of the negatives of using xpath. 我建议阅读http://saucelabs.com/resources/selenium/css-selectors ,以简要了解一些示例,并且可能还涉及http://saucelabs.com/resources/selenium/selenium-xpath-marks-现场才能看到一些使用XPath的底片。

You should separate 2 buttons by this innertext. 您应使用此内文分隔两个按钮。 Try 尝试

//a[contains(@class, 'add-new') and contains(., 'Add New')]

for "Add New" button. 用于“添加新”按钮。 And: 和:

//a[contains(@class, 'add-new') and contains(., 'Add New Campaign')]

for "Add New Campaign" button. 用于“添加新广告系列”按钮。

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

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