简体   繁体   中英

Watir can't find element but it's present

I'm transitioning from the following environment:

chromedriver 2.27
google-chrome 58
selenium-webdriver 3.0.5
watir 6.0.2
page-object 2.0.0

to the this environment:

chromedriver 2.36
google-chrome 63
selenium-webdriver 3.9.0
watir 6.7.3
page-object 2.2.2

I already have my cucumber tests ready and working on the old environment and some of them are working properly in the new enviorment, but for some reason there is one test that refuses to find a certain element and gives me the following exception (even though the element is clearly present in the page and in the DOM):

timed out after 30 seconds, Element not present in 30 seconds (Watir::Wait::TimeoutError)

the element is defined as such:

table(:some_table, :css => 'table.table.table-condensed.table-striped.sortable tbody')

Did anybody encounter the same issue, I've been looking a lot and couldn't find a solution to this problem.

EXAMPLE:

Feature file:

Feature: HTML Tables

  Scenario:  Demo of reproducing the problem

    Given I am logged in to HTML Tables page
    Then within 10 I expect to see Alfreds Futterkiste in the table

Steps:

Given(/I am logged in to HTML Tables page$/) do 
  visit HtmlTables
end

Then(/^within (.+) I expect to see (.+) in the table$/) do |time_limit, element|
  on HtmlTables do |page|
    actual_element = page.search_for_element(element, time_limit)
    expect(actual_element).to match(element), "Expected element to match: #{element}; got: #{actual_element}"
  end
end

Page:

class HtmlTables
  include PageObject

  page_url "https://www.w3schools.com/html/html_tables.asp"

  table(:html_table,                   :css    => 'tbody')

  def search_for_element(element, time_limit)
    Retriable.retriable on: [Watir::Exception::UnknownObjectException],
                      tries: time_limit.to_i/10,
                      base_interval: 10 do
      html_table_element.each do |row|
        return row[0].text if row[0].text == element
      end 
    end
  end
end

It gives out the same exact problem - I think it has a problem locating using css and/or xpath.

The problem is that Watir is being told to find a table element, however the :css locator is finding a tbody element. The current version of Watir validates that the element found has the matching tag name. While I thought Watir had always been doing this, it's possible that there were some edge cases addressed in the recent re-factoring of the Locator class.

Ideally you would be able to switch the table accessor to a tbody one. However, there isn't one implemented. Instead, you'll need to use the generic element one:

element(:html_table, :tbody, css: 'tbody')

Iterating the rows of the table will require explicitly calling #trs :

html_table_element.trs.each do |row|
  return row[0].text if row[0].text == element
end   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM