html - 使用 selenium webdriver 和 ruby​​ 时 div 的 CSS 选择器

标签 html ruby css-selectors selenium-webdriver web-testing

我在使用 selenium webdriver 时使用 CSS 选择器来查找元素,但是为下面的 div 查找元素似乎很困难。

<div class="class1 class2" dd:btnfloatingstyle="top-right" dd:entitytexttype="resultval" id="_78891a36-3d75-432e-9906-760bd918e7cb" contenteditable="true"></div>

为了使用 css 选择器查找元素,我通常这样做:

$driver.find_elements(:css, 'div.classname')

但在这种情况下我有 2 个类名,但我没有取回元素:

   $driver.find_elements(:css, 'div.class1 class2') or
   $driver.find_elements(:css, 'div.class1 > div.class2') or
   $driver.find_elements(:css, 'div.class1 + div.class2')

我是不是遗漏了什么或者有没有其他方法可以找到这个元素??

最佳答案

尝试如下:

$driver.find_elements(:css, 'div.class1.class2')

举个例子,我使用了 Ruby_on_Rails wikipedia 链接并编写代码以从提到的链接访问下面的 html 表。

HTML

 <table class="wikitable sortable jquery-tablesorter" align="left">
 <caption>Version history</caption>
 <thead>
    <tr>
       <th class="headerSort" title="Sort ascending">Version</th>

ruby 代码

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://en.wikipedia.org/wiki/Ruby_on_Rails"
elem = driver.find_element(:css, 'table.wikitable.sortable.jquery-tablesorter')
p elem.tag_name
# => "table"
p elem.find_element(:tag_name, 'caption').text
# =>  "Version history"

关于html - 使用 selenium webdriver 和 ruby​​ 时 div 的 CSS 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17976420/

相关文章:

html - 做 3x 列模板的正确方法

html - 线性渐变结合图像背景不显示

jquery - 使用 "toggle"函数的简单 jQuery 测试,未按预期运行

arrays - 如何将数组的元素移动到数组的开头

python - 如何将独立的 Python 脚本集成到 Rails 应用程序中?

ruby - 我什么时候应该使用 "class Object"、 "class Module"、 "module Kernel"而什么都不用?

java - 使用 css 选择器在表中使用多个元素获取元素

javascript - amCharts 中 x 轴上的标签丢失

javascript - Jquery 选择器 - 多个项目

javascript - CSS :before/:after Selectors in IE 6, 7