I get this far, opening the firefox browser, navigating to google, and finding the google search element
irb(main):001:0> require 'selenium-webdriver'
=> true
irb(main):002:0> driver = Selenium::WebDriver.for:firefox
=> #<Selenium::WebDriver::Firefox::Marionette::Driver:0x..fb3c81796cc82b708 browser=:firefox>
irb(main):003:0> driver.navigate().to("http://www.google.com")
=> nil
irb(main):188:0> driver.find_element(:name, "q").send_keys "fff"
=> nil
irb(main):112:0> driver.find_element(:name, "btnK");
=> #<Selenium::WebDriver::Element:0x5fb450f4379c50ce id="d767311c-27a2-3544-8f11-e4edc9736588">
irb(main):113:0> driver.find_element(:name, "btnK").attribute('value');
=> "Google Search"
But I can't manage to click it!
irb(main):114:0> driver.find_element(:name, "btnK").click
Traceback (most recent call last):
16: from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/w3c/bridge.rb:552:in `execute'
15: from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/bridge.rb:166:in `execute'
14: from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:62:in `call'
13: from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/default.rb:104:in `request'
12: from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:84:in `create_response'
11: from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:84:in `new'
10: from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/response.rb:32:in `initialize'
9: from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok'
8: from clickElement#chrome://marionette/content/listener.js:1209:5
7: from navigate#chrome://marionette/content/listener.js:409:13
6: from navigate/<#chrome://marionette/content/listener.js:410:13
5: from clickElement/<#chrome://marionette/content/listener.js:1210:14
4: from interaction.clickElement#chrome://marionette/content/interaction.js:130:11
3: from webdriverClickElement#chrome://marionette/content/interaction.js:159:11
2: from ElementNotInteractableError#chrome://marionette/content/error.js:286:5
1: from WebDriverError#chrome://marionette/content/error.js:178:5
Selenium::WebDriver::Error::ElementNotInteractableError (Element <input name="btnK" type="submit"> could not be scrolled into view)
irb(main):115:0>
It says that the element "could not be scrolled into view" !
I can do driver.find_element(:tag_name, "body").send_keys :page_down; which would page down. Which is mentioned Looking at ruby selenium documentation for send_keys https://www.rubydoc.info/gems/selenium-webdriver/Selenium%2FWebDriver%2FElement%3Asend_keys . I see a list of key codes https://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/Keys#KEYS-constant . And also listed https://github.com/SeleniumHQ/selenium/blob/master/rb/lib/selenium/webdriver/common/keys.rb . Or I can page down manually. But scrolling (at least in the normal sense of the term) doesn't seem to be the issue, I still get that error. Maybe it's in a frame I can switch to and I don't know which.
The button is of course visible. It's just a regular google search page.
I can find the button fine but I can't see how to click it, as .click isn't working for me.
added
in reply to a comment asking me if the google suggestions box is obfuscating the button. I can see in the browser that the button isn't obfuscated 'cos sometimes I have done escape manually, or clicked the background window(to get rid of that. I am using IRB so running each statement manually so I have time to do that). But I also did escape with the code, and escape works to get rid of the suggestion box, but still same error when trying to click the button
While discussing this problem in the comments, I wrote some C# code to demonstrate how we wait for clickable in C#
new WebDriverWait(Driver, TimeSpan.FromSeconds(5)).Until(ExpectedConditions.ElementToBeClickable(By.Name("btnK"))).Click();
In the process of writing the C# code I posted, I think I see what the issue is. When you start typing in the search box, a dropdown appears that contains the suggested searches. This covers the "Google Search" button that you are trying to click... BUT another "Google Search" button appears at the bottom of the dropdown itself. You should print a count of the (:name, "btnK") element(s)... I'm assuming it will print 2 (if a proper wait is added). From there, you just need to click the one that is on top.
Another option would be to send a \r\n at the end of your search string and avoid this whole issue... or you could just navigate to the final search URL and save even more headaches and time.
Added note from barlop
To clarify, this has nothing to do with the suggestions popup showing(and having to get rid of the popup).. and nothing to do with having a wait (Since I was working with IRB, I was naturally waiting). And I'd already clicked outside or hit escape to get rid of the popup. The issue was, and Jeff's answer alerted me to this - Even after the popup is gone, there are still two buttons with that btnK name. So, using find_elements rather than find_element, and running .click on the second one works! (Also, using find_elements(plural) for the name attribute makes sense, since as noted in an answer herehttps://stackoverflow.com/questions/5518458/does-a-name-attribute-have-to-be-unique-in-a-html-document. the name attribute is not unique / not a unique identifier.
This is what I told you to use WATIR, If you had used WATIR, this problem wouldn't have arrived. The problem in your code, it's not waiting for visibility. Recently Chrome Driver has added implicit wait for click as well, so it would wait for visibility if you set the implicit wait. Otherwise move to WATIR which is a good wrapper which doesn't wait via driver, In WATIR waiting for element status happens from local language binding, so use this selenium code
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
driver.manage.timeouts.implicit_wait = 10
driver.navigate().to("http://www.google.com")
driver.find_element(:name, "q").send_keys "fff"
driver.find_element(:name, "btnK").click
Remember this selenium code wouldn't work for firefox because implicit wait for click is not added yet for firefox.
Since WATIR is handling the timing from local language binding, it doesn't matter whether you use Chrome or Firefox, it would perfectly work
WATIR Code (Default is Chrome)
require 'watir'
b=Watir::Browser.new
b.goto 'www.google.com'
b.text_field(name: 'q').set 'fff'
b.button(name: 'btnK').click
If you want to drive Firefox
b=Watir::Browser.new :firefox
But you can do the click with javascript (sending javascript to the browser).
b=driver.find_element(:name, "btnK")
driver.execute_script("arguments[0].click();",b)
Jeff explains why the javascript method(selenium with javascript), works on either button but the non-javascript method(selenium without javascript) works only on one button, he wrote "Both of the buttons would work, the JS click will click on any element no matter where it is... or if it's visible, etc. Selenium was designed to interact with the page as a user would so it was unable to click on the element due to the error."
Related
I cannot get Capybara to click on the SCA/3DS ‘Complete authentication’ button when running RSpec tests. Similar tests which do not trigger SCA pass just fine, and if I run VNC to view what Firefox is doing, the button is visible and I can click it myself in the browser.
My problem seems very similar to what’s discussed in the comments here, but the solutions do not work: I have tried changing the browser used, and flattening the iframe traversal.
Test code:
scenario "SCA required" do
create_payment_method(account, payment_method: "stripe", last_four: "1234")
visit "/billing"
click_on "Enter Card Payment"
within "#main-content" do
within_frame(find("iframe")) do # Stripe payment form is in an iframe.
find("input#Field-numberInput").set("4000002760003184") # SCA-required test card.
find("input#Field-expiryInput").set("1234")
find("input#Field-cvcInput").set("123")
find("input#Field-postalCodeInput").set("12345")
end
end
find("button#submit").click
# Stripe nests the popup in several layers of iframes.
stripe_frame = find("body > div > iframe") # Popup is prepended to the body element.
switch_to_frame(stripe_frame)
challenge_frame = find("iframe#challengeFrame")
switch_to_frame(challenge_frame)
fullscreen_frame = find("iframe.FullscreenFrame")
switch_to_frame(fullscreen_frame)
click_on "Complete authentication"
switch_to_frame(:top)
expect(page).to have_content "ends in 3184"
end
Is there some way to debug what Selenium is doing under the hood here? I don’t see any movement on the page when running click_on "Complete authentication", but if I click on the button myself in the Firefox instance being controlled by Selenium it does work.
Running click_on "Complete authentication" returns the element clicked, which appears to be the expected element when I drop into Pry and call native.dom_attribute("id").
I can see an error of some kind in the browser container’s logs:
1654078084345 Marionette WARN TimedPromise timed out after 500 ms: stacktrace:
TimedPromise/<#chrome://remote/content/marionette/sync.js:239:19
TimedPromise#chrome://remote/content/marionette/sync.js:224:10
interaction.flushEventLoop#chrome://remote/content/marionette/interaction.js:431:10
webdriverClickElement#chrome://remote/content/marionette/interaction.js:179:31
It’s a bit odd because it mentions #chrome but this is a headless Firefox instance.
Assuming no error is returned by the click_on call then I'm guessing the button is being clicked before it's ready to be clicked. You can test that by sleeping for a few seconds before calling 'click_on'/navigating through the frames. If that fixes it then you'd need to look at what changes on the button to indicate that the page has finished whatever work it's doing and the button is ready to be clicked.
I have solved this by clicking on the button directly with JavaScript:
execute_script(%Q{ document.querySelector("button#test-source-authorize-3ds").click() })
However, this does not in any way explain why click_on is not working, and if anything makes it more strange that it is not. If anyone has a better solution or a way to dig into why Capybara/Selenium are failing then that would be welcome.
when i search for the button it exists but when i try to click on it, it cannot find it and times out. How do I fix it?
i have the updated chrome driver. i have all the gems at the latest levels.
irb(main):189:0> ie.button(value:"OK").html
=> "<input type=\"button\" onclick=\"javascript:makeList('1591993');\" value=\"OK\">"
irb(main):190:0> ie.button(value:"OK").exists?
=> true
irb(main):191:0> ie.button(value:"OK").click
2021-02-05 18:07:41 WARN Selenium [DEPRECATION] Selenium::WebDriver::Error::ElementNotVisibleError is deprecated. Use Selenium::WebDriver::Error::ElementNotInteractableError (ensure the driver supports W3C WebDriver specification) instead.
Traceback (most recent call last):
8: from C:/Ruby26-x64/bin/irb.cmd:31:in `<main>'
7: from C:/Ruby26-x64/bin/irb.cmd:31:in `load'
6: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
5: from (irb):191
4: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/watir-6.16.5/lib/watir/elements/element.rb:145:in `click'
3: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/watir-6.16.5/lib/watir/elements/element.rb:790:in `element_call'
2: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/watir-6.16.5/lib/watir/elements/element.rb:803:in `rescue in element_call'
1: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/watir-6.16.5/lib/watir/elements/element.rb:752:in `raise_present'
Watir::Exception::UnknownObjectException (element located, but timed out after 30 seconds, waiting for #<Watir::Button: located: true; {:value=>"OK", :tag_name=>"button"}> to be present)
There may be a high chance that there are two buttons with the values 'OK' and when you write ie.button(value:"OK").click it issue the click on the first button but it may be invisible. So if there are two buttons, the button you are seeing may be a second button. So first you execute the below code
p ie.buttons(value:"OK").count
And see if it prints 2 or 3(greater than one), if it prints, then you are actually interacting with the first button that's invisible. So you have to find out which button you would like to click, If count is exactly 2, then you could write the following code.
ie.buttons(value:"OK").last.click #I used buttons here not button
This will issue the click on the last button. If button count is more than two, then you could pass the index of the button to click the target button like below
ie.buttons(value:"OK")[3].last.click
If the button is exist but not clickable:
It's not visible: check its first by open the page manually, adjust its CSS
It's blocked by something. That may be a transparent block with higher z-index on it, or the button is blocked partially. Try to move the button to another wide-open space and run the script again.
the fire_event method did the trick
ie.button(value:"OK").fire_event "onclick"
This gets the job done in most situation, will click present one
#b.buttons(text: "OK").select(&:present?).first.click
Sometimes watir click fails (for good reasons) so you do it with javascript,
#b.buttons(text: "OK").select(&:present?).first.fire_event :click
But you can also try sending enter keystroke
#b.buttons(text: "OK").select(&:present?).first.send_keys :enter
you can verify its working if you see expected button receiving the click
I am trying to automate tests in Ruby using the latest Watir-Webdriver 0.9.1, Selenium-Webdriver 2.53.0 and Chrome extension 2.21. However the website that I am testing has static headers at the top or sometimes static footers at the bottom. Hence since Watir auto-scrolls an element into view before clicking, the elements get hidden under the static header or the static footer. I do not want to set desired_capabitlites (ElementScrollBehavior) to 1 or 0 as the websites I am testing can have both - static header or static footer or both.
Hence the question are:
1) Why does Watir throw an exception Element not clickable even when the element is visible and present? See ruby code ( I have picked a random company website for an example) and the results below.
2) How can I resolve this without resorting to ElementScrollBehaviour?
Ruby code:
require 'watir-webdriver'
browser = Watir::Browser.new :chrome
begin
# Step 1
browser.goto "shop.coles.com.au/online/mobile/national"
# Step 2 - click on 'Full Website' link at the bottom
link = browser.link(text: "Full website")
#check if link exists, present and visible?
puts link.exists?
puts link.present?
puts link.visible?
#click on link
link.click
rescue => e
puts e.inspect
ensure
sleep 5
end
puts browser.url
browser.close
Result:
$ ruby link_not_clickable.rb
true
true
true
Selenium::WebDriver::Error::UnknownError: unknown error: Element is not clickable at point (460, 1295). Other element would receive the click: div class="shoppingFooter"...div
(Session info: chrome=50.0.2661.75)
(Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.10.5 x86_64)>
http://shop.coles.com.au/online/mobile/national
thanks!
You can do a click at any element without getting it visible. Check this out:
link.fire_event('click')
BUT It is very very very not good decision as far as it will click the element even if it is not actually visible or in case when it is just impossible to click it (because of broken sticky footer for example).
That's why much better to wait the fooler, scroll the page and then click like:
browser.div(id: "footerMessageArea").wait_until_present
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
link.click
The sticky footer is blocking webdriver from performing the click, hence the message that says 'other element would receive the click'.
There are several different ways you can get around this.
Scroll down to the bottom of the page before the click
Hide/Delete the sticky footer before any/all link clicks
Focus on an element below the element you want to click before you perform the click
I Guess your element is visible in the screen.
Before clicking on the element first you have to scroll the webpage so that element is visible then perform the click. Hope it should work.
I had similar issue,
I just used following javascript code with watir:
link = browser.link(text: "Full website")
#browser.execute_script("arguments[0].focus(); arguments[0].click();", link)
Sometimes I have to use .click! which i believe is the fire_event equivalent. Basically something is layered weird, and you just have to go around the front end mess.
I have setup Appium - Capybara and was able to run automation suites on physical device, but facing issue with few methods
I was able to successfully run Capybara methods like
fill_in field, :with => text
click_button button_text
expect(page).to have_content(text)
But facing issues with below method ( it works on regular chrome on laptop but not on mobile )
page.first(:link, link_text).click
can you please help me to understand if appium capybara supports all the capybara methods or only few of them.
Below is the error message
undefined method `click' for nil:NilClass (NoMethodError)
In the above code, first the link with link_text is being searched on the page. If no link is found then nil is returned.
Thus, to make this code work, we need to wait for the link with the link text to appear on the page and then click it.
So you can use any one of the below mentioned code before clicking the fink
page.should have_content(link_text)
page.find_link(link_text)
If the above code doesn't works then you can also try increasing the default wait time as shown below:
Capybara.default_wait_time = 30
page.should have_content(link_text)
page.first(:link, link_text).click
Capybara.default_wait_time = DEFAULT_WAIT_TIME
DEFAULT_WAIT_TIME for Capybara tests is set in your environment file.
Hope this helps :)
undefined method 'click' for nil:NilClass (NoMethodError) is telling you that #first isn't returning an element (#first returns immediately with a matching element, or nil if there is none). Is there a reason you are using first rather than find?? If there is only one matching link on the page you should really prefer find over first (in this case find_link), or try to scope to a part of the page where the would only be one link, since it will throw an error explaining why it didn't find the link (rather than returning nil) and also has waiting behavior for elements to show up (first can have the same behavior but it doesn't by default)
I'm automating IE with watir, and I want to know what html element(s) are clicked (selected). Can this be done using watir? win32ole? In a last chance, without ruby?
Something like:
Click a button -> button with id=213 and class=btn_class was clicked.
Click a text field -> text field with id=123 and value=my_text was clicked.
Try one of the recorders, Selenium IDE for example.
I'm not sure if I completely understand either, but would a custom function like this work?
def click(item, how, what)
#browser.item(how, what).click
puts "#{item} with #{how}->#{what} was clicked"
end
click("button", ":id", "awesome")
Edit: If you're attempting to identify page elements so that you can then use them in a Watir script, the Developer Toolbar is perfect for this application, much like Firebug for Firefox.
http://www.microsoft.com/download/en/details.aspx?id=18359
Your comment to me & your response to Zeljko appear to contradict each other. If you want to use WATIR for this task, the code above will execute a mouse click and post the information to console. The only other way to get information is to locate the object in WATIR and fish for more details:
object = #browser.button(:name, /myButton/)
object.id
object.title
object.status
object.height, etc.
or
#browser.divs.each do |div|
puts div.id, div.title
end
I'd recommend a strong look at the capabilities of the various developer tools, such as are provided with Chrome, Firefox, and IE. those are generally the best means to get this kind of information.
Watir is really about driving the browser, and getting info out of the DOM. it's not really setup to report on manual interactions with the browser.