How to click on a button with just a text? - ruby

I have a button that has no class, id just a text
<button> Click me </button>
I tried the following:
driver.find_element(:xpath, "//button[contains(text(), 'Click me'")]
driver.find_element(:xpath, "//button(text() = 'Clck me'")
None of these work. Could someone put me in the right path?

Your code looks like syntactically incorrect, You should try as :-
driver.find_element(:xpath, "//button[normalize-space(text())='Click me']")
or
driver.find_element(:xpath, "//button[contains(text(), 'Click me')]")
Note :- Your button text seems as contains extra white spaces. So for avoiding these white spaces you should use normalize-space() in your xPath
Hope it will help you..:)

You have errors in your xpath. The correct one is:
# use button[] instead of button()
# you have weird order of ", ' and )
# space in the text 'Click me' also be considered ' Click me '
driver.find_element(:xpath, "//button[text() = ' Click me ']")

If it is not your typo in question, Try after changing:
driver.find_element(:xpath, "//button[contains(text(), 'Click me'")]
to
driver.find_element(:xpath, "//button[contains(text(), 'Click me')]")
Otherwise, find all the element on the page with tag name as "button" then traverse through the list of those elements to get the desired one.
Eg Code:
Get all buttons:
buttons = driver.find_elements(:class=> 'button');
Get the button with text 'Click Me'
clickMeButton = buttons.select{|el| el.text == 'Click Me'}.first
click on the button:
clickMeButton.click

Related

Cannot get past a modal overlay with Selenium, using Ruby

I am trying to click a button which appears in modal overlay pop up. The element of the button cannot be found using an xpath, which worked for all the steps before. If I try to find it by selecting a css class, it tries to click the overlay. But finding the overlay by itself, isn't possible.
The button that I need to click:
enter image description here
The script:
`
#click export as csv
$wait = Selenium::WebDriver::Wait.new(:timeout => 15)
input = wait.until {
element = driver.find_element(:xpath, '/html/body/div[6]/div/div/div/div[3]/div/button/div')
element if element.displayed?
}
driver.find_element(:xpath, '/html/body/div[6]/div/div/div/div[3]/div/button/div').click
`
I tried a few different things:
Selecting the xpath
`
#click export as csv
$wait = Selenium::WebDriver::Wait.new(:timeout => 15)
input = wait.until {
element = driver.find_element(:xpath, '/html/body/div[6]/div/div/div/div[3]/div/button/div')
element if element.displayed?
}
driver.find_element(:xpath, '/html/body/div[6]/div/div/div/div[3]/div/button/div').click
`
Selecting the css, which gave an interesting error:
`
#click export as csv
$wait = Selenium::WebDriver::Wait.new(:timeout => 15)
input = wait.until {
element = driver.find_element(:css, '.button-spinner-container')
element if element.displayed?
}
driver.find_element(:css, '.button-spinner-container').click
`
The error (it seems to click something else):
0x0000000103082a88 chromedriver + 4123272: element click intercepted: Element ... is not clickable at point (321, 96). Other element would receive the click: ...

Cypress click on button on same line with the text element

I'm having trouble with clicking on the button that is multiple times on the website and has the same class.
First I need to locate a specific name and click on a button that is on the same line/row.
I tried to implement the bellow but it can find the button.
cy.contains('user2#gmail.com')
.parents('tr')
.find('button')
.click()
For example, I need to locate the name "Agranov Anton" and click on the clock icon next to it. The name changes every time.
The clock icon is the 2nd button, so add .eq(1) to pick that one
cy.contains('user2#gmail.com')
.parents('tr')
.find('button')
.eq(1)
.click()
You can also specify the <tr> element at the first step
cy.contains('tr', 'user2#gmail.com')
.find('button')
.eq(1)
.click()
BUT maybe the clock icon uses an <a> element, if so then use
cy.contains('tr', 'user2#gmail.com')
.find('a')
.eq(1)
.click()
Assuming that the name Agranov Anton is a td element and also the button is the correct selector of the button that you are clicking, you can do something like:
cy.contains('td', 'Agranov Anton')
.parent('tr')
.within(() => {
cy.get('button').click()
})

Ruby Shoes: how to replace button text with an image/icon

I am trying to write a small Shoes app which contains a button. For example
Shoes.app :title => "Buttons" do
button_next = button "Next"
button_prev = button "Previous"
end
Now, instead of the texts "Next" or "Previous" there should be icons on the buttons: a green arrow, showing to the right for the button_next and a green arrow showing to the left for the button_prev.
Of couse I already have the icons as .jpg-files but I can't figure out how to replace the text with the icons on the buttons.
For now I found out this way:
Shoes.app do
next_image = image "next.jpg", :width => 50, :height => 35
next_image.click do
alert "Hey!"
end
end
It works in the way that I can click the image and a new window, saying "Hey!" appears. But it is not ok, because I miss the typical button-appearance like changing of the color when hovering over it, or the press-down-effect when clicking on it.
So my question is: how can I create a real button in Ruby Shoes and replace the name of the button with an icon? Any ideas?

how to click on text area to send long text using ruby with selenium web driver

I tried xpath and id
getting `until': timed out after 10 seconds (no such element (Selenium::WebDriver::Error::TimeOutError)
I am using rubymine editor.
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until { #driver.find_element(:xpath => "//*[#id='j_id0:pb:j_id35']") }
#driver.find_element(:xpath => "//*[#id='j_id0:pb:j_id35']").send_keys "test send sms"
Text area element is placed on bottom of the page.Do I need to scroll down the page and click and sendkeys in the text area.
in the below code I am trying to find nearest element to the text box and do scroll down later click on text area and sendkeys.But even it's not working..
#wait = Selenium::WebDriver::Wait.new(:timeout => 10)
#wait.until { #driver.find_element(:name => "j_id0:pb:j_id33") }
#scroll = #driver.find_element(:name => "j_id0:pb:j_id33")
#scroll.location_once_scrolled_into_view
Please help on this..
Thanks!!
I don't have idea on rudy but can help you logically..
1) first scroll the page from your code to the text area.
2) select the text area by id, xpath or etc. driver.findelement(by.id(...)).sendkey(..........);
Thanks Shukla!#sourabh shukla
Mine inside has frame so I need to switch to frame that is why I am not able ciclk that element!!
#driver.switch_to.frame("06618000000Crrp"#this is id);
Then my element found and entered text!!

cursor not follow mouse click in the text input field in Firefox

I created a simple Jquery slider:
<div id="slider"></div>
and attach one text input field to the slider handler by using Javascript:
$( "#slider" ).slider();
var value = "<input id='handler1InputValue' type = 'text' spellcheck = 'false' style='position:absolute;width:70px;height:15px;text-align:center;left:-2px;font-size:10px;display:block;' ></input>";
$( ".ui-slider-handle" ).html(value);
$( ".ui-slider-handle" ).unbind();
$( "#handler1InputValue" ).val(77777);
But in Firefox, the cursor does not follow mouse click in the text input field, mouse selection doesn't work as well, while everything works in Chrome side.
Anybody know how to solve the problem in Firefox side? And you can try the test sample here: slider with text input field

Resources