Selenium onChange not working - events

I have tried a number of things to try and get Selenium to pick up an 'onchange' event from a drop down menu, none of which has worked.
The offending HTML is:
<select onchange="doOpperation(this.options[this.selectedIndex]); this.selectedIndex = 0;" name="opps_ondemand" id="opps_ondemand">
<option value="none" id="ondemand">Mark as...</option>
<option cmd="blah1" value="add">Something</option>
<option cmd="blah2" value="remove">None</option>
</select>
I have read that Selenium IDE doesn't record some on* events, and so it would be wise to use fireEvent():
$this->click("opps_ondemand");
$this->select("opps_ondemand", "label=Mark as...");
$this->click("//option[#value='add']");
sleep(3);
$this->fireEvent("//select[#id='opps_ondemand']", "change");
However, this does not work (with or without the fireEvent). I have also tried using
$this->fireEvent("locator", "click");
instead of
$this->click("locator");
but this did nothing.
Selenium does not complain about these locators not existing so I am assuming it can see the select/option elements fine. The problem seems to be the onChange event.
Does anyone know how to resolve this?
Thanks.

I encountered exactly this problem, but in IE only (Firefox and Google Chrome works fine for me)
I found the solution to be manually forcing the update using JavaScript through Selenium's runScript. Some ways to do that can be found here:
How do I programmatically force an onchange event on an input?
For example, if I have jQuery in my Web page, I would do this:
$this->select('IDOfSelectElement', '*some label*');
$this->runScript("$('#IDOfSelectElement').trigger('change')");

tried this?
$this->fireEvent("opps_ondemand", "onchange");
fireEvent(element_id, event_to_trigger);

Related

Need help to switch to an iframe using cypress

I use Cypress to automate logging in to a web application, protected by an iFrame.
In my Selenium I can use a command to switch to iFrame:
driver.switchTo().frame(driver.findElement(By.xpath(".//*#id='app']/iframe")));
After that I can access iFrame elements as well.
But with Cypress, I don't know the method to switch to frame?
Cypress doesn't give you an out of the box solution to work with iframes like Selenium or other tools. But thanks to the huge community, a lot of workarounds are available to work with iframes. One such way is:
1.Go to cypress/support/command.js and write:
Cypress.Commands.add('getIframe', (iframe) => {
return cy.get(iframe)
.its('0.contentDocument.body')
.should('be.visible')
.then(cy.wrap);
})
2.In the tests write:
cy.getIframe('selector')
If you want to learn more about iframes and cypress, you can read this well written blog post by Gleb Bahmutov.

Unable to click OK button in pop-up in webdriver using ruby

Unable to click on OK button
I tried like this but it is not clicking.
b.find_element(name: "OK").click #----> it is not clicking
How to interact with this type of pop-us in ruby.
<input class="copybutton" type="button" title="Copy" onclick="copyErrText()" name="copy" value=""/>
<!-- *** Added: copy button *** -->
<button class="button" onclick="disp()" name="OK" value="OK" type="button">OK</button>
You can try one of the following it may help you.
ele = find_element(name: "OK")
driver.action.move_to(ele).click(ele).perform
or
driver.execute_script("arguments[0].click();",ele)
or
driver.execute_script("disp();")
You've tagged this Watir, but your syntax is Selenium. If you are using Watir, it would look like this:
browser.button(name: 'OK').click
If there are funky javascript things going on, you can fire an event to interact with it. Use this power sparingly.
browser.button(name: 'OK').fire_event :click
Hope this helps.
driver.switch_to.alert.accept

Not able to select element using SelectElement class in FireFox 48, Im using the latest drivers (marionette) using Ruby

So the only automation I have ever done is what I kind of self learned at my current job using cucumber with ruby. I have had issues with my tests since upgrading selenium and gekko driver for Firefox 48+.
My CSS looks like the following:
<select id="scheduled-task-type" name="maintenance_task[task_type]">
<option value="16">Engine Runtime</option>
<option value="10">GPS Odometer</option>
<option value="13">Idle Time</option>
My previously working command was
#browser.select(id: "scheduled-task-type").select "GPS Odometer"
This no longer seems to function. It doesn't throw an error but it does not change the drop down either. It actually seems to think it correctly performed the action.
#browser.select(id: "scheduled-task-type").select "GPS Odometer"
=> "GPS Odometer"
I have tried to get it to click the drop down to open it (this works) but when I try to get it to make a selection after that using .click such as
#browser.select(id: "scheduled-task-type").click
#browser.select(id: "scheduled-task-type").option(text: "GPS Odometer").click
It still doesn't work and also doesn't throw an error.
If you want to select an option using pure selenium in ruby, you can use following code snippet:
#browser.find_element(:css, "select#scheduled-task-type option[value=\"10\"]").click
#browser is your selenium webdriver instance and find_element is the method to find any element on a webpage. Hope this helps.

Automation using Watir button elements

So I have been experimenting with Watir automation scripts using Ruby. I have tried to experiment with different websites, like Twitter, Gmail, and Yahoo. Here is the catch: I am able to open a browser, login, and get to the home page. On all of these whether it is compose a new email or tweet every time I select it with the appropriate ID or Class, it throws an error in the terminal like this...
.rvm/gems/ruby-2.2.1/gems/watir-webdriver-0.8.0/lib/watir-webdriver/elements/element.rb:533:in assert_element_found': unable to locate element, using {:title=>"compose", :tag_name=>"button"} (Watir::Exception::UnknownObjectException)
from /Users/xxx/.rvm/gems/ruby-2.2.1/gems/watir-webdriver-0.8.0/lib/watir-webdriver/elements/element.rb:505:inassert_exists'
from /Users/xxx/.rvm/gems/ruby-2.2.1/gems/watir-webdriver-0.8.0/lib/watir-webdriver/elements/element.rb:114:in click'
from yahoo_mail.rb:18:incompose_email'
from yahoo_mail.rb:27:in `'
My question is, are there elements that you simply cannot click or select using Watir automation?
EDIT: How would I be able to hit this to be more specific? I seem to be getting the same results on Yahoo, Gmail, and Twitter when it comes to composing anything after getting logged in. So I how would I hit this button?
button id="global-new-tweet-button" type="button" class="js-global-new-tweet js-tooltip btn primary-btn tweet-btn js-dynamic-tooltip" data-placement="bottom" data-component-term="new_tweet_button">
The HTML for the Compose button is basically the following (NOTE: id attribute removed):
<button tabindex="0" data-action="compose" title="Compose" class="btn btn-compose">
Your stacktrace indicates that you're using this locator:
{:title=>"compose", :tag_name=>"button"}
If you change :title=>"compose" to :title=>"Compose", you should be in business.
Have you tried the following code?
button(id:'global-new-tweet-button').when_present.click
Maybe you were clicking the button without checking if it is already present on the page.

ERROR: Unable to locate element

I have to test a dynamic app using the ZK framework and Selenium does not identify the id's from the different elements, so can't enter text in the textboxes or select an element in a list (elements from a database)
Whatever I use (xpath or css selector) nothing works, always the same error
Does anyone know how can I fix my problems?
I'm using Selenium IDE 1.9.0
Netbeans IDE 7.1.1
And Firefox 16.0.2
Thanks
The Html code is:
button id="zc_subdossierzulButton_8" class="butt z-button-os" style="border-style: solid;border-width: 1px;border-color: #ED0000;" type="button">Rechercher
And the Java code i tried is:
driver.findElement(By.cssSelector("zc_subdossierzulButton_8.butt"));
don't work
this:
driver.findElement(By.cssSelector("butt z-button-os"));
don't work
and this:
String cssSelector = "[class='butt z-button-os']";
driver.findElement(By.cssSelector(cssSelector)).clear();
driver.findElement(By.cssSelector(cssSelector)).sendKeys("c");
Please go through the following documentation and try using different options like
driver.findElement(By.id("coolestWidgetEvah"));
OR
driver.findElements(By.className("cheese"));
OR
driver.findElement(By.tagName("iframe"));
OR
driver.findElement(By.name("cheese"));
OR
driver.findElement(By.linkText("cheese"));
OR
driver.findElement(By.partialLinkText("cheese"));
OR
driver.findElements(By.xpath("//input"));
http://seleniumhq.org/docs/03_webdriver.html#locating-ui-elements-webelements

Resources