Cucumber/Capybara 从特定类中选择按钮?类中、按钮、Cucumber、Capybara

2023-09-08 00:07:57 作者:渥著手鈊dé你,很满足

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

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>

而另一个按钮是:

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

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

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)

老实说,我选择哪一个并不重要,因为它们都具有相同的功能.我已经看到你可以在哪里使用 xpath?但是我们现在不应该使用css选择器或其他东西吗?(因为那是最新的方式?)我可能错了......

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

我试图避免仅使用rails"的解决方案,因为并非我测试的所有网站都是基于 rails 的.

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 找到按钮(您可以使用 Google Chrome 复制它们:右键单击 -> 检查 -> 右键单击​​您的元素 -> 复制 css 或 xpath)

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