问题
I'm using watir to perform automated testing for an application and trying to click on a submit button once I select options for select list and fill up the form.However on clicking the button, it throws element not found error
The html snippet for button tag
<button type="submit" class="md-primary md-raised md-button md-default-
theme" ng-transclude=""><span class="ng-binding
ng-scope">Submit</span><div style="" class="md-ripple-container">
</div>
</button>
The ruby script
require "watir"
require "watir-webdriver"
browser = Watir::Browser.new :firefox
browser.goto 'https://54.69.254.137/webui#/landing'
browser.driver.manage.window.maximize
browser.button(:class =>'sign-in md-button md-default-theme').click
browser.text_field(:id =>'input_001').set('abcadmin@example.com')
browser.text_field(:id =>'input_002').set('password')
browser.button(:class =>'md-primary md-raised md-button md-default-
theme').click
browser.input(:id =>'input_002').when_present.click
browser.element(aria_label:'What do you want to do?').when_present.click
browser.element(:id =>'select_option_00G').when_present.click
browser.element(aria_label:'About what?').when_present.click
browser.element(:id =>'select_option_00P').when_present.click
browser.textarea(:id =>'input_00N').when_present.set('Discuss about
javascript and later test the application??')
browser.button(:class =>'md-primary md-raised md-button md-default-
theme').click
It throws the following error
Selenium::WebDriver::Error::ElementNotVisibleError: Element is not
currently visible and so may not be interacted with
I tried using
browser.button(:class =>'md-primary md-raised md-button md-default-
theme').when_present.click
,but it will throw time out error.Not able to rectify the error .Plz help !!
回答1:
You are trying to select on more than one class at once. To do that you would have to use a css selector instead of a class selector, which would look something like this:
browser.button(:css =>'.md-primary.md-raised.md-button.md-default-
theme').click
The class option will only work if you select a single class name, e.g.
browser.button(:class =>'md-primary').click
回答2:
I run into this error quite a bit where I work, when the Front-end devs want to stylize a boring looking element (such as a button or checkbox).
In this particular case the span element is covering/styling the button element. just click on the span instead.
browser.span(:text, 'Submit').click
回答3:
Element not visible means that the element was found, but is either hidden or obscured in such a way that a real user would not be able to click the button directly.
Two potential causes for that error, one is that something else covers the element you want. The other is that there are more than one element that matches in the dom, and the one being selected (unless you give an index value, watir will take the first one it finds) is not visible.
The fast way to determine which is the case is to first get a count of matching elements.. that is easy by inserting a puts into your script right before it tries to click the button (on two lines for readability)
buttons_found = browser.buttons(:class =>'md-primary md-raised md-button md-default-theme').size
puts "I found #{buttons_found} matching buttons"
If that returns anything other than 1, then potentially you are selecting a hidden button. Maybe look instead to select it by having watir look inside a container element, or find a more unique way to identify the button you want.
Note that using class combinations can be a bit brittle since classes are often used to drive CSS formatting. That can create two problems, one is that any button with the same formatting will likely have the same combination of classes and if the visual design changes the classes can also change For example if more than one was found, then you could try selecting the button by it's text, such as:
browser.button(:text 'Submit').click
If there is only a single element found, then look to see if some other element might be masking the one you want, and try clicking on that instead.
来源:https://stackoverflow.com/questions/31537550/element-not-visible-error-for-button-element-in-watir