Capybara - Click element by class name - ruby

For what seems to be a simple question I've been on this for a stupidly long time and can't seem to find anything on Google. I have this button I need to click which has no id but a class is included
<button class="filter-case-studies" onclick="initBootpag(filterForContentType('CASE STUDIES', searchHits))" type="button">
<b>CASE STUDIES</b>
(2)
</button>
I've tried using click_on which I now know is only for links and buttons so of course won't work. This is what I have so far:
When(/^I filter the results to only see case studies$/) do
click_on('filter-case-studies')
end
I've also tried page.find('filter-case-studies').click, this too doesn't work.
page.find(:class, 'filter-case-studies').click defualts to :css so this also failed for me.
Is there no way to click an element by the class name in Capybara?
Thanks in advance for the help.

The standard way of doing this in Capybara is
find('button.filter-case-studies').click
In relatively recent versions of Capybara you should also be able to do
click_on(class: 'filter-case-studies')

find('.filter-case-studies').click as recommended here https://robots.thoughtbot.com/write-reliable-asynchronous-integration-tests-with-capybara#find-the-first-matching-element

I had a button that could not be found (Unable to find visible link or button nil with classes [close-modal]) using the methods above.
This worked for me: page.execute_script('$.find(".close-modal")[0].click()')

click_on('.filter-case-studies')
You need the . selector for classes, and # for ids.

Thanks to Mr Schutte for the idea to use . selectors.
I had to use page.find(:class, '.filter-case-studies').click in the end. The absolute navbar got in the way of the button so I then had to include page.execute_script "window.scrollBy(0,500)" to complete the test.

Related

Need help on clicking an element (Element is not clickable at point (62, 459)) - Capybara Ruby Selenium Automation

I am having below error message in my console while trying to click on a button element:
unknown error: Element is not clickable at point (62, 459).
Other element would receive the click: <i class="foo foo-chase-lemon font-size-13"></i>
Here's my code below:
#object = Page.new
#object.wait_until_btn_element_visible
#object.btn_element.click
I have tried with retry 5 times to click on it using rescue but didn't help.
Below code also didn't work where i tried to move to that element before click.
Capybara.page.driver.move_to.(#object.btn_element).perform
Any solution will be greatly appreciated.
I tried increasing the resolution/ scrolling the window. None of them worked on this specific scenario. These solution might work for others.
However, I resolved the issue by clicking the button using javascript "execute_script" method in my automation script.

using watir for a facebook app

recently I'm using watir/ruby to publish on facebook automatically. In simple words I managed to write a sentence in the text field, but I can't click on the button "Publish".
I tried in different ways:
browser.link(:value => '1').click
browser.button(:value => "Publish").click
browser.li(:xpath, '//INPUT[#type="submit"]').click
browser.link(:href =>'javascript:doSubmit()').click
browser.a(:text =>"Publish").click
but I get the error messages: unable to locate element, or element not visible.
I also tried to write:
browser.button(:value => '1').exists?
but I got the answer "false". Can anyone help me?
Thank you very much
The problem is tons of Javascript on the page.
You should wait until your element appears.
Use corresponding functions here
Example:
browser.button(:value => "Publish").wait_until_present.click
Firstly, posting information is not scraping information. I've definitely automated tests for Facebook with a test user account, which was absolutely in line with the TOS.
Is this an element that is showing up in a pop up window? If so you need to do something like: browser.windows.last.use { browser.a(text: 'Publish').click }

Selenium webdriver can't find button

EDIT:
I have cleaned this up a bit.
I have a button that looks like this:
<input id="applyRuleButton" class="Button" name="filtersContainer:applyRuleButton"
value="Apply" onclick="wicketShow('applyRuleButton--ajax-indicator');var
wcall=wicketSubmitFormById('id256', '?wicket:interface=:23:form:filtersContainer:applyRuleButton:
:IActivePageBehaviorListener:0:&wicket:ignoreIfNotActive=true',
'filtersContainer:applyRuleButton' ,function() { ;wicketHide('applyRuleButton--
ajax-indicator');}.bind(this),function() { ;wicketHide('applyRuleButton--
ajax-indicator');}.bind(this), function() {return
Wicket.$$(this)&&Wicket.$$('id256')}.bind(this));;; return false;" type="submit">
Firebug:
<input id="applyRuleButton" class="Button" type="submit"
onclick="wicketShow('applyRuleButton--ajax-indicator');var
wcall=wicketSubmitFormById('id2ee',
'?wicket:interface=:29:form:filtersContainer:applyRuleButton::IActivePageBehaviorListener:0
:&wicket:ignoreIfNotActive=true', 'filtersContainer:applyRuleButton' ,function() {
;wicketHide('applyRuleButton--ajax-indicator');}.bind(this),function() {
;wicketHide('applyRuleButton--ajax-indicator');}.bind(this), function() {return
Wicket.$$(this)&&Wicket.$$('id2ee')}.bind(this));;; return false;" value="Apply"
name="filtersContainer:applyRuleButton">
I'm trying to click it and have tried pretty much everything for 2 days, webdriver does not find the element, IDE does find it:
//This was my first approach, it should work.
It works in IDE, but not Webdriver:
driver.findElement(By.id("applyRuleButton")).click();
//then perhaps this should do the trick, hint: It doesn't:
WebElement element3 = driver.findElement(By.id("applyRuleButton"));
JavascriptExecutor executor3 = (JavascriptExecutor)driver;
executor3.executeScript("arguments[0].click();", element3);
Ok, Id not working, I get it.
Then this should work at least:
driver.findElement(By.xpath("//table/tbody/tr/td/div/div/table/tbody/tr[6]/td/input[#id='applyRuleButton']")).click();
It feels like I am missing something obvious here, some help please?
Additional information:
I have added a 5 second wait, the page is completely loaded.
This button is located in a table:
The Xpath is
/html/body/div[4]/div[2]/form/div[3]/div/div/table/tbody/tr/td/div/div/table/tbody/tr[6]/td/input
Webdriver error, no matter what I throw at it, is: Unable to locate element
I have used both 'click' and 'submit', still no success.
I think in this case there are two possibilities :
Either there is another element having same id/xpath.
OR Element present in another iframe.
Is the button visible. Selenium click (latest firefox 26 and latest webdriver 2.39.0) does not sometimes implicitly scroll; Or it may not scroll it fully. So scroll it into view - Scroll Element into View with Selenium and then it should work.
Note Selenium Best Practise try to use By.Id,By.CSSSelector and if nothing gets use By.Xpath in the order of priority. ( Use the FireFinder, FireBug plugin to test XPath or CSS)
This might be a synchronization issue. Such issues can be solved using smart waits.
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists((By.Id("applyRuleButton"))));
WebElement element3 = driver.findElement(By.id("applyRuleButton"));
And that should work perfectly fine.
There is absolutely nothing wrong with your selector. I just don't think you're invoking the click correctly.
Try:
driver.findElement(By.id("applyRuleButton")).click();
If this doesn't work, then you might have to invoke a WebDriverWait since you have this question marked as [ajax]
Could you post the entire html?
As a simple experiment, I took the html snippet that you posted and wrote a short python script that invokes selenium:
from selenium import webdriver
br = webdriver.Firefox()
br.get("put your path to snippet here")
button = br.find_element_by_id("applyRuleButton")
print button.get_attribute("name")
button.click()
br.close()
I can find the button and extract the attribute "name" which prints "filtersContainer:applyRuleButton". This is admittedly a very limited experiment, but it suggests that the issue is related to not being where you think you are on the page.
Try this:
driver.findElement(By.Name("filtersContainer:applyRuleButton"));
If this doesn't help, check whether this button is located in any other frame. If so, you may have to find and move your focus to that frame and then locate this button.
First try to identify the button by writting correct xpath using firebug, if you are able to identify button by that xpath then use that xpath while writing your script.
driver.findElement(By.xpath("//input[# type='submit' and # id='applyRuleButton'")).click();
This is ajax application use proper explicit/ webdriver wait till the button gets downloaded
I see that this thread is old, but I was looking at it today (Sept/2021) as I was having the same problem: I could see the name / id/ type of the button, but it would never be found.
I discovered that when I had clicked in a prior link, it opened a new tab in my browser, but Selenium did not change the focus to the new tab, so it couldn't find the ID of the button I was looking for.
I solved it with :
driver.find_element_by_id("export").click() #driver
time.sleep(2)
driver.switch_to.window(driver.window_handles[1]) # Change focus to the new tab
driver.find_element_by_id("0001btn").click() #click
driver.close() #close new tab
switching to a specific frame helped me to resolve the same issue. (python + selenium)
I installed the Selenium Recorder extension on chrome and recorded my steps, and found out that the recorder had a step to select a frame = 0, so adding
self.home_page.driver.switch_to.frame(0)
self.home_page.click_on_element_by_id("clickSubmit")
solved the problem.

Is "enabled?" method available on Watir and/or Page-Objects for a link?

Yesterday i was working on determining is this link was or not enabled, waiting until enabled in order to click it. I'm using Cucumber + Ruby + Watir + Page-Object gem. The link is very similar to:
<a id="the_link" href="#" disabled="disabled">Proceed with your order</a>
Once you fill some fields, the link is enabled and the source changes to:
<a id="the_link" href="#">Proceed with your order</a>
The idea is to wait until the link is enabled in this way, which works with buttons:
def click_on_link
Watir::Wait.until { self.the_link_element.element.enabled? }
self.the_link
end
...but does not work with the link. I made it work determining if the attribute exists, this way:
def click_on_proceed_form
Watir::Wait.until { !self.the_link_element.element.attribute_value('disabled') }
self.proceed_form_submit
end
Looking for the "enabled?" method at the Watir documentation here or at the Page-Object gem here, it seems that is available for all elements. But looking for the same method in the Watir documentation here, seems it's only available for Buttons, Selects and Inputs.
So, i wonder is there is an "enabled?" method for links (anchors) and, if it exists, how to use it. Can you help clarify this issue, please? Thank you very much!
Watir-webdriver does not support the enabled? method for links. I believe this is because the disabled attribute is not a standard attribute for links.
On the other hand, Watir-classic does support the enabled? method for links. However, it will always return false (again because links cannot be disabled).
Therefore, I think your approach is correct (unless you want to monkey patch the link elements to support the enabled?).
However, you should try to avoid using Watir-webdriver directly where possible. The page-object gem has its own methods for waiting and getting attribute values:
def click_on_proceed_form
wait_until{ !the_link_element.attribute('disabled') }
proceed_form_submit
end

How to hover over (mouseover) an element in Selenium Ruby?

Anyone know how to hover over an element in Selenium Ruby Webdriver?
My code is like this:
el = driver.find_element(:css => "#foo")
driver.move_to el # How do I trigger a mouseover event on this element?
I'm using selenium-webdriver gem with Firefox in Linux 32-bit.
I used driver.action.move_to(el).perform which differs ever so slightly from the other answers, so I thought I would include it for completeness sake.
Turns out the answer is:
driver.move_to(el).perform
I forgot the .perform.
This works for me:
driver.mouse.move_to el
You need to use Selenium's Action Builder to access more complex actions like hovering (which is what seanny123's answer is demonstrating).
Also, if you are working with a hover, odds are you will need to dynamically wait for it to display before taking your next action (e.g., using an explicit wait).
I put together an example on how to do this -- you can see the full write-up here.
To hover an element:
driver.action.move_to(element).perform
# e.g.
driver.action.move_to(driver.find_element(css: 'a')).perform
To hover an element at a specific location:
driver.action.move_to(element, mouse_x, mouse_y).perform
# e.g.
driver.action.move_to(driver.find_element(css: 'a'), 100, 100).perform

Resources