黄瓜/水豚选择特定类别的按钮?(Cucumber/Capybara Selecting Button from Specific Class?)

所以我遇到了一个问题,我试图点击一个按钮...不幸的是有相同的文字(IE页面上有2个按钮,相同的文字,基本上是一个保存按钮)

让我们假装按钮文本只是“保存”。

我注意到他们有不同的课程。

<button data-action="submit" class="btn btn-primary btn-save">Save</button>

而另一个按钮是:

<button name="button" type="submit" class="btn btn-primary set-right"> <i class="glyphicon glyphicon-floppy-disk"></i> Save </button>

我知道glypicon只是一个图标集......但他们都似乎属于同一个班级? 但有不同的类名? (对不起,我不熟悉Rails)

由于它们都具有相同的功能,所以我选择哪一个并不重要。 我已经看到你可以在哪里使用xpath? 但是我们不应该现在使用css选择器吗? (就像那是最新的方式?)我可能是错的....

我可以使用像这样的东西:

find(:xpath, '//button[@class="btn-save"]').click

我试图避免“铁轨”唯一的解决方案,因为并非我测试的所有网站都基于轨道。

So i've ran into an issue, im trying to click a button...which unfortunately has the same text (IE there are 2 buttons on the page which the same text, basically a save button)

Lets pretend the button text is just "Save".

I did notice that they had different classes.

<button data-action="submit" class="btn btn-primary btn-save">Save</button>

whereas the other button is:

<button name="button" type="submit" class="btn btn-primary set-right"> <i class="glyphicon glyphicon-floppy-disk"></i> Save </button>

I know glypicon is just an icon set....but they both seem to belong to the same class? but have different class names? (Sorry im not familiar with Rails)

It doesn't honestly matter which one I select as they both have the same function. I've seen where you can use xpath? but aren't we supposed to use css selectors or something now? (As in thats the newest way?) I may be wrong....

Could I use something like:

find(:xpath, '//button[@class="btn-save"]').click

Im trying to avoid "rails" only solutions, as not all the websites I test on are rails based.

最满意答案

你有各种可能性:

1)按查找按钮

find(button.btn.btn-primary.btn-save).click

2)通过其css选择器xpath找到按钮(您可以使用谷歌浏览器复制它们:右键单击 - >检查 - >右键单击您的元素 - >复制css或xpath)

find(:css, "your button css selector").click find(:xpath, "your button xpath").click

You have various possibilities:

1) Find the button by its class

find(button.btn.btn-primary.btn-save).click

2) Find the button by its css selector or xpath (you can copy them using Google Chrome: right click -> Inspect -> right click on your element -> copy css or xpath)

find(:css, "your button css selector").click find(:xpath, "your button xpath").click

更多推荐