Iterate over all fields of a form with selenium in ruby

I am trying to verify that my form fields have an associated label using Selenium, but I am having problems capturing all form fields on the page. get_all_fields gets only text fields; I also have no way to capture passwords, radios, flags, etc.

I tried something like this:

num_fields = Integer(selenium.get_xpath_count("//input"))

1.upto(num_fields) do |field_number|
  input_id = selenium.get_attribute("//input[#{field_number}]@id")

  selenium.element?("css=label[for=#{input_id}]")

end

The problem is that // input [1] does not work; inputs are nested in different layouts depending on the page.

Is there a way to use a selenium locator for a common capture of the first, second, etc. input?

+3
source share
2 answers

Try to use //body/descendant::input[#{field_number}].

+2
source

, XPath, ; //body/descendant::input[#{field_number}] ; descendant::input , _.

XPath, - , .

, xpath . , IE, , . , selenium.get_html_source WebStandardsChecker .

+1

Source: https://habr.com/ru/post/1754261/


All Articles