4

我在使用 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')

我错过了什么还是有其他方法可以找到这个元素?

4

1 回答 1

7

尝试如下:

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

举个例子,我使用了Ruby_on_Rails维基百科链接并编写了一个代码来从提到的链接访问下面的 html 表。

HTML

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

红宝石代码

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"
于 2013-07-31T17:24:18.283 回答