在水豚中选择具有多个类别的元素 [英] Selecting element with multiple classes in Capybara

查看:57
本文介绍了在水豚中选择具有多个类别的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Capelebara用Selenium编写自动化代码。
我的HTML中包含以下元素,我想在Capybara中单击该元素。

I'm writing automation code in Capybara with Selenium. I have the following element in my HTML, and I wanna click this element in Capybara.

<a href="#" class="classA classB">click me</a>

目前,工作方式类似于以下。

At the moment, the way worked is something like following.

find('.classA', :text=>"click me").click

但是我想从两个类的名称中选择元素

But I wanna select the element from the names of the two classes like this

find('a.classA.classB').click
click_on('a.classA.classB')

我知道我们可以触发JavaScript代码,但这并不明智。

I know we can get javascript code fired, but this is not smart.

page.execute_script('$("a.classA.classB").click()')


推荐答案

根据示例,您可以通过 xpath

You can search an element by xpath

来搜索元素,如下所示应该工作

based on your example, seems like the following should work

//div[contains(@class, 'classA') and contains(@class, 'classB')]

您还可以使用 css

(:css, ".classA.classB")

这篇关于在水豚中选择具有多个类别的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆