Press TAB and then ENTER key in Selenium WebDriver with Ruby - ruby

I am doing automated testing using Selenium WebDriver with Ruby. I need to click a button. I cannot get the button element by id or css or xpath as the button is transparent. I would like to use Tab and Enter key to press the button.
I can use Tab key to get the button as below:
#element.send_keys :tab
#element --> any javascript element visible in the browser
But how do I use the Enter key on the button?
Basically I need to achieve press Tab key and then press Enter key to click the button.
I am using Selenium WebDriver #driver = Selenium::WebDriver.for :firefox

In Ruby user1316's code looks like
driver.action.send_keys(elementVisible, :tab).send_keys(elementVisible, :return).perform

Keeping in mind the excerpt :
I can use tab key to get the button as
#element.send_keys :tab
#element --> any javascript element visible in
the browser
but how do i use the enter key on the button??
In order to use the enter key on the button, you could try one of the solution provided using Ruby here. This basically talks about sending the :return value and not the :enter value i.e #element.send_keys :return and some additional information.
UPDATED:
I could provide some code in Java which tries to implement the problem conceptually using the info provided here. You could try to translate for the corresponding Ruby Selenium API.
The Code:
Actions builder = new Actions(driver);
builder.sendKeys( elementVisible, Keys.TAB).sendKeys(Keys.RETURN);
Action submitTheTransperentButton = builder.build();
submitTheTransperentButton.perform();

use Selenium::WebDriver::Keys::KEYS[:tab]
ref: https://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/Keys

send ENTER in ruby:
#browser.action.send_keys("\n").perform

Related

Unable to click on link using Link text/Partial Link text using ruby

I am new here and even to Ruby and Selenium. I am trying to click a link on web page which has following code:
<a> target="mainFrame" href="dynamic_Utility_Index.htm">Dynamic Utilities</a>
So basically I want to click on this dynamic utilities. The script which I have written so far is:
require 'selenium-webdriver'
require 'win32ole'
driver = Selenium::WebDriver.for:firefox
driver.manage().window().maximize();
driver.navigate.to 'xyz.com'
wait = Selenium::WebDriver::Wait.new(:timeout =>10) # seconds
#Click on Dynamic Utilities
wait.until{driver.find_element(:link_text,'dynamic_Utility_Index.htm').click}
puts "Clicked"
I have even used link, partial_link_text in place of link_text but keep getting the following error
(Unable to locate element: {"method":"link
text","selector":"dynamic"})
(Selenium::WebDriver::Error::TimeOutError)
I am using Ruby and Selenium Web driver.
Did you try with:
wait.until{driver.find_element(:link_text,'DYNAMIC UTLITIES').click}
?? Sometimes I had to use the text link by capitalized.
Another option could be:
driver.find_element(:xpath, "//a[contains(#target, 'mainFrame')]").click
Hope works for you :D
The item you consider to be a link text (dynamic_Utility_Index.htm) actually just a value of href attribute. Actual link text is "Dynamic Utilities". Also you can use xpath locator //a[#href="dynamic_Utility_Index.htm"] instead of search by link text:
wait.until{driver.find_element(:xpath,'//a[#href="dynamic_Utility_Index.htm"]').click}

How to manage frame in ruby selenium webdriver

I am working with RUBY selenium webdriver (not java, not c#, etc). Please, the solutions must be in ruby. So, I have the frame with Id "_wicket_window_3". To locate this element, I apply:
driver.switch_to.frame driver.find_element(:id, "_wicket_window_3")
My frama has the buttons "Adjuntar", "Exportar" and "Terminar".
Now, how can i manage the frame ? I want to click the button "Adjuntar","Terminar" or "Exportar".
If I try there:
frame = driver.switch_to.frame driver.find_element(:id, "_wicket_window_3")
frame.click_button("Adjuntar")
The response is: Undefined method.
Please, help me !!!
Regards, Agustín.
You can't manage the iframe you switched to as you planning.
When you do
driver.switch_to.frame driver.find_element(:id, "_wicket_window_3")
Your context start to be inside this iframe, all operations you will now do on driver, will be inside this iframe.
So, after the switch do:
driver.click_button("Adjuntar")
It should click on the button.
I dont think click_button is the right command, that's why the error you get is undefined method
you have to switch to the frame then look for the element of that button as you did before to look for the frame, then click it
something like
driver.switch_to.frame driver.find_element(:id, "_wicket_window_3")
driver.find_element(:id, "id_of_the_button_you_want_to_click").click
Note used :id there but you can use whatever the locator you want, :css, :xpath, :class whatever

how to double-click on a cell in table with Selenium Ruby Webdriver

I'm trying to use below codes to double-click on a cell in a table in my web application (I tried to do Click two times with the hope that they are equal to double-click). However, the cell is not clicked at all, but I expect that after double-clicking on the cell, the text field will be displayed for me to edit info of that cell.
#driver.find_element(:xpath => "//table[#id='gridabc']//tr[1]/td[9]").click
#driver.find_element(:xpath => "//table[#id='gridabc']//tr[1]/td[9]").click
I'm using Selenium Ruby Webdriver. Please help guide me a way to resolve this. Thanks much.
##example Double click an element
el = driver.find_element(:id, "some_id")
driver.action.double_click(el).perform
Code shamelessly copied from the documentation :)

Controlling new tab to operate using Watir? (Ruby)

I'm using watir for automated testing and after running through some tables, a chart then gets opened up in a new tab. But watir doesn't seem to recognise the new current tab and continues to search through the original browser tab.
Is there any way of telling watir which tab you want to be using?
Watir does not care if a new page opens in a new window or in a new tab, so use window switching API to switch to the new tab:
browser.window(:title => "annoying popup").use do
browser.button(:id => "close").click
end
More information: http://watirwebdriver.com/browser-popups/
You can use this code
browser.windows.last.use
this will set watir focus to newly or last opened tab or window, and after completion of use of this new tab/window just tell watir to close that tab/window
browser.windows.last.close
There are 3 primary selectors for windows:
:title - typically the easiest
:url - often used with a Regexp value
:element - a unique element might be the least brittle (new as of Watir 6.18!)
browser.window(title: 'new window')
browser.window(url: /my_page.html/)
browser.window(element: browser.div(id: 'my-element'))
Locating by index is no longer supported
More information: Watir Browser Windows
#browser.windows[x].use
Replace 'x' with the tab number you'd like to use. #browser.windows is a Watir Collection, which can be treated like an Array for this purpose.

How can I know what element is clicked in IE?

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.

Resources