【问题标题】:CSS selector for div while using selenium webdriver and ruby使用 selenium webdriver 和 ruby​​ 时 div 的 CSS 选择器
【发布时间】:2013-08-01 08:02:30
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    尝试如下:

    $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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多