1
votes

Is there any way on how to click a hidden button by an overlapped input text field, for example if you go to www.google.com and enter a text to search for, selenium can not find the "Search Google" button because it is hidden by the autocomplete of the text field.

Thanks.

1

1 Answers

2
votes

You can bypass the validation by using #click! instead of #click. Basically, this triggers the click via JavaScript rather than through standard Selenium commands.

browser = Watir::Browser.new
browser.goto('www.google.com')
browser.text_field(name: 'q').set('watir')
browser.button(name: 'btnK').click!

If you are only using Selenium, you can do:

btn = driver.find_element(name: 'btnK')
driver.execute_script('arguments[0].click();', btn)

As discussed in the comments, you could also close the suggestions box before trying to click the button. You can do this by moving focus to any other element - eg the first link on the page. Depending on what you're testing, this may or may not have value.

browser.text_field(name: 'q').set('watir')
browser.link.focus # move focus to any other element so suggestions close
browser.button(name: 'btnK').click