Unable to click on 'Cancel' button - ruby

I'm writing automated script using Selenium WebDriver with Ruby. In the case, I've to click on 'Cancel' button and following is the html code for it:
<div class="ui-dialog-buttonset">
<button class="otherButtonClass" type="button" role="button" aria-disabled="false">
<span class="ui-button-text">Rename</span>
</button>
<button class="cancelButtonClass" type="button" role="button" aria-disabled="false">
<span class="ui-button-text">Cancel</span>
</button>
</div>
For clicking on 'Cancel' button, I wrote following:
driver.find_element(:xpath, "//button[#class='cancelButtonClass']").click
here click action doesn't happen. I tried sleep, wait.until { element.displayed? } still issue wasn't resolved. The error thrown is 'Element is not visible and hence may not be interacted with'
However, if I perform click action on 'Rename' button, it works:
driver.find_element(:xpath, "//button[#class='otherButtonClass']").click
Please help me to understand why this is happening. I'm confused, 'Rename' and 'Cancel' have similar html code and still clicking on 'Rename' passes and clicking on 'Cancel' fails. Why like this?

You can try the below :
script = <<-end
element = arguments[0];
element.setAttribute('aria-disabled','true');
return element;
end
# select the 'Cancel' button element
elem = driver.find_element(:css,'div.ui-dialog-buttonset>button')[1]
# setting the 'aria-disabled' to true
elem = driver.execute_script(script,elem)
#after enabling the css attribute 'aria-disabled' click on the
#cancel button
elem.click

Using CSS selection will not be a perfect solution in case if the Button CSS are dynamic for any hover actions. Also the Simple way of selecting given element is using the following xpath.
driver.find_element(:xpath, "//span[text()='Cancel']").click

Related

How to capture current set of classes used on an element

I have two elements on the page: button1 and button2. One of them has class active set on an initial load.
I want to grab button1 and check if active is set. If set, then for my tests I want to use button2, if not I stick with button1.
For my test, I need to test the switch action between two buttons, hence why I need to use the button which is not an active one.
Each button does have a text, i.e. <div class="active" data-cy="button1">Button 1</div>, so I was also thinking that maybe a different option would be to grab a button1 with class active set and check if it exists? Not sure if that is possible...
How can the above be achieved?
You want to avoid conditional processing in the test, try to use a selector that targets what you want.
Given
<button class="active">Button 1</button>
<button>Button 2</button>
use a :not() selector
cy.get('button:not(.active)')
.should('have.text', 'Button 2')
If there's other button, also not active but not wanted,
<button class="active">Button 1</button>
<button>Button 2</button>
<button>Ignore me</button>
you can add a filter
cy.get('button:not(.active)')
.filter((index, button) => ['Button 1', 'Button 2'].includes(button.innerText))
.should('have.text', 'Button 2')
If you really meant the active attribute,
<button active>Button 1</button>
<button>Button 2</button>
cy.get('button:not([active])')
.should('have.text', 'Button 2')

Ruby / Watir : how to click on a disabled button (Watir::Exception::ObjectDisabledException)

I want to make a program in Ruby that create a github repository. Everything is all right, but when i want to click the button 'create repository' after filling the repository name, nothing happened and the program stop with a timeout error.
This is the html code of the disabled button :
<button type="submit" class="btn btn-primary first-in-line" data-disable-with="Creating repository…" disabled="">
Create repository
</button>
And the html code of the enabled button :
<button type="submit" class="btn btn-primary first-in-line" data-disable-with="Creating repository…">
Create repository
</button>
And this my ruby program
repo_name = gets.chomp
repo = browser.text_field(id: 'repository_name')
repo.set(repo_name)
browser.driver.manage.timeouts.implicit_wait = 3
create_button = browser.button(type: "submit")
create_button.wait_until(&:enabled?).click
I'am pretty sure that my pb comes that when i'm landing on the page, the button is disabled, and even if i'm filling the repository_name input, my prog can't access to the button.
So do you have a solution about that ? Or maybe do you know if there is an other pb ?
Edit :
When here is the code without the waiting commands :
repo_name = gets.chomp
repo = browser.text_field(id: 'repository_name')
repo.set(repo_name)
create_button = browser.button(type: "submit").click
And when I run it, I'v got a 'Watir::Exception::ObjectDisabledException' error
("element present, but timed out after 30 seconds, waiting for #
<Watir::Button: located: true; {:type=>"submit", :tag_name=>"button"}> to be enabled (Watir::Exception::ObjectDisabledException)"
I think below code might work for you
browser.text_field(id: 'repository_name').set("repo_name")
browser.send_keys(:tab)
And as Justin mentioned in his answer, click the create repository button as below:
browser.button(type: "submit", visible: true).click
The problem is that there are multiple submit buttons on the page. You can see this by retrieving a collection of buttons:
# Button text:
browser.buttons(type: 'submit').map(&:text_content)
#=> ["Set status", "Sign out", "Create repository"]
# Disabled status:
browser.buttons(type: 'submit').map(&:disabled?)
#=> [true, false, false]
browser.button(type: "submit") returns the first submit button on the page, which is the disabled "Set status" button.
The "Create repository" button is actually the last one on the page. A more specific locator for the button is required. Some options:
# By text
browser.button(text: 'Create repository')
# By visibility (since the other 2 are hidden by default)
browser.button(type: "submit", visible: true)

How to click on a Popup Confirmation alert (Selenium Webdriver / Ruby)

I'm trying to click on a popup confirmation alert using Selenium Webdriver / Ruby, but even with the xpath I can't click on the OK or Cancel button.
(popup window: https://imgur.com/2E8dqKe)
HTML code:
<div>
<a onclick="$find('confirm1545915453689').close(true);" class="rwPopupButton" href="javascript:void(0);"><span class="rwOuterSpan"><span class="rwInnerSpan">OK</span></span></a>
<a onclick="$find('confirm1545915453689').close(false);" class="rwPopupButton" href="javascript:void(0);"><span class="rwOuterSpan"><span class="rwInnerSpan">Cancel</span></span></a>
</div>
Code I tried:
browser.find_element(:xpath => '//*[#id="confirm1545919261219_content"]/div/div[2]/a[2]/span/span').click
and
browser.find_element(:xpath => '//td[.="Cancel"]').click
Thanks for ur time
do you have any errors while trying to click?
try following locator:
browser.find_element(:xpath, "//span[#class='rwInnerSpan' and text()='OK']").click;
browser.find_element(:xpath, "//span[#class='rwInnerSpan' and text()='Cancel']").click;

This XPath expression is not working with Selenium WebDriver

I am using Google Chrome as webdriver.
The Sign Up button code is:
<button type="submit" class="signupbtn btn_full btn btn-action btn-block btn-lg">
<span class="ink animate" style="height: 488px; width: 488px; top: -215px; left: -118px;"></span>
<i class="fa fa-check-square-o"></i>
Sign Up
</button>
The error code is:
Exception in thread "main" org.openqa.selenium.WebDriverException:
unknown error: Element is not clickable at point (681, 658)
My XPath code for execution is:
driver.findElement(By.xpath("//*#id='headersignupform']/div[9]/button")).click();
However, it is not executing the script and is throwing the above error. As you can see, in the console it is locating the button with my code in the console.
You need to use focus or scroll to that element.
You also might have to use an explicit wait.
WebElement element = driver.findElement(By.xpath("//*#id='headersignupform']/div[9]/button"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
If it still does not work, use JavascriptExecutor:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
It seems the Sign Up button has an overlay. So to interact directly with the Sign Up button, we need to use the help of JavascriptExecutor as follows:
WebElement button = driver.findElement(By.xpath("//button[#class='signupbtn btn_full btn btn-action btn-block btn-lg']"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", button);
You could use an action class for the same:
WebElement element = driver.findElement(By.xpath("//*[#id='headersignupform']/div[9]/button"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
The second thing is it's always advisable to use an exact tag instead of *[#id =""].

Mouse Hover in selenium Ruby using capybara

Searching from many forums i got the code to hover the mouse over an element
.I want to hover the mouse over a thumbnail image.
i put html element ".thumbnail" as class inside find_element method .I dont know which parameter should i place inside find_element method?
My html element is given below.
<a class="thumbnail">
<span class="badgeFeatured">featured</span>
<img alt="" src="https://do3dm75n3e02m.cloudfront.net/arts/preview_3ds/14/original/open-uri20121128-5897-wh90rf_1354148717.png20121128-5897-194vld7-0?1354148717">
<p>Flourish Happy Birthday</p>
</a>
el = driver.find_element(:class => "thumbnail")
driver.move_to.(el).perform
Due to this problem it does not move mouse over the thumbnail image.
In Webdriver(java), we can perform mouse over action like this:
Actions actions = new Actions(driver);
WebElement imageSpan = driver.findElement(By.className("badgeFeatured"));
actions.moveToElement(imageSpan);

Resources