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