Selenium click() or isSelected() are not executed on RadioButton's input element - xpath

I have radio buttons that are located inside a table, such as:
<tr id="radiofield-1080-inputRow">
<td class="x-form-item-body" id="radiofield-1080-bodyEl" colspan="3">
<input type="button" id="radiofield-1080-inputEl" class="x-form-field" autocomplete="off">
<label id="radiofield-1080-boxLabelEl" class="x-form-cb-label">My Label</label>
</td>
</tr>
I do find the input element, by the following code:
xPath = String.format("//tr/td[contains(#id,'%s')][contains(label,'%s')]/label", xType, text);
webElement = webDriver.findElement(By.xpath(xPath));
but isSelected() or click() doesn't seem to work on it.
Do you have any suggestion?

Haven't used selenium in a while but from a quick google it looks like you should try the isChecked and check/uncheck methods.
Here's the Javadoc but for some reason can't get a decent link, obviously check on the Selenium object. If you're using a different version or if I misunderstood something sorry.
http://selenium.googlecode.com/git/docs/api/java/index.html

In your code snippet problem with locator.
Use any of the below below locators
By.cssSelector("input[id*='radiofield-']");
By.id("radiofield-1080-inputEl")
By.xpath("//tr/td[contains(#id,'radiofield-')]/input")

Try clicking on the "input" instead of the "label".
xpath of input:
"//input[#id='radiofield-1080-inputEl']"
If the input id is not always the same, you can try this, the location of input will be based off the label:
//label[text()='My Label']/preceding-sibling::input

Thanks for all your answers.
My finding concluded with the following:
ExtJS implement radiobutton and checkbox as button, therefore the selenium isSelected() is not functioning.
There is a need to implement isSelected(), as suggested at:
How to check if extjs checkbox is selected in selenium?
The click() does the work, as it is a button.
Thanks again, Michal

Related

can not find element using watir webdriver

I am trying to click on the button.
<input data-ember-action="797" value="Search Now" class="cta-1st clrboth primary search-button firepath-matching-node" type="button">
but it keep showing an error of not finding the element to click.
it finds all the elements until the end but when it comes to "click" it throws an error. My code is like this:
#browser.div(:id=>'ember730').section(:class=>'tab-pane fade active in').form(:class=>'flight-search ').input(:class=>'cta-1st clrboth primary search-button').click
Am I missing something?
I am using Watir
Thank you for all the help here. the problem was, I had a wrong reading in one of the elements.
I was missing this part.
#browser.div(:id=>'ember730').section(:class=>'tab-pane fade active in').form(:class=>'flight-search ').text_field(:class=>'cta-1st clrboth primary search-button firepath-matching-node').click
As there is value="Search Now" section of type="button" here, following simple code snippet should do. I have made this working using Watir in my project..
#browser.button(value: "Search Now").click

Excel VBA to Deal With AJAX

I am practicing to use excel vba to download information from website: http://mops.twse.com.tw/mops/web/t05sr01_1
But I have no idea how to download the data behind click button, as the image shown: http://i.stack.imgur.com/KZHiZ.jpg
I excerpt its web code as below. Could anyone explain me how to code in excel vba to get its data?
Thank you very mush.
Web code:
<td style='text-align:left !important;' nowrap>鴻海</td>
<td style='text-align:left !important;'>105/01/05</td>
<td style='text-align:left !important;'>11:41:00</td>
<td style='text-align:left !important;'>說明媒體報導</td>
<td><input type='button' value='詳細資料' onclick="document.fm_t05sr01_1.SEQ_NO.value='1';document.fm_t05sr01_1.SPOKE_TIME.value='114100';document.fm_t05sr01_1.SPOKE_DATE.value='20160105';document.fm_t05sr01_1.COMPANY_NAME.value='?E??';document.fm_t05sr01_1.COMPANY_ID.value='2317';document.fm_t05sr01_1.skey.value='2317201601051';document.fm_t05sr01_1.hhc_co_name.value='?E??';ajax1(this.form,'table01');">
You haven't shown how you are getting the html.
You can use a CSS selector.
General for first input button
input[type=button]
This says element(s) with input tag having attribute type whole value is 'button'
You apply with the querySelector method, or querySelectorAll if more than one match and then use index for required element.
ie.document.querySelector("input[type=button]").Click
If in an HTMLDocument variable e.g. htmlDoc then
htmlDoc.querySelector("input[type=button]").Click

A way around Element cannot be scrolled into view - Watir-webdriver with Ruby

So, we have the following code in our page:
<div class="toggle-wrapper">
<input id="HasRegistration_true" class="registration_required toggle" type="radio" value="True" name="HasRegistration" data-val-required="The HasRegistration field is required." data-val="true">
<label for="HasRegistration_true" class="">On</label>
<input id="HasRegistration_false" class="registration_required toggle" type="radio" value="False" name="HasRegistration" checked="checked">
<label class="checked" for="HasRegistration_false">Off</label>
</div>
These are 2 radio buttons. 'On' and 'Off'. 'Off' is the default value.
Using Watir-webdriver and Ruby, we want to select the 'On' radio button. We do so like this:
browser.radio(:id => "HasRegistration_true").set
But in doing so, we get the following error:
`WebElement.clickElement': Element cannot be scrolled into view:[object HTMLInputElement] (Selenium::WebDriver::Error::MoveTargetOutOfBoundsError)
We know Selenium 2 scrolls the page to the element, so trying to scroll down is useless.
We are always using the latest releases of watir-webdriver and ruby.
We can't change the HTML of the page since we're QA engineers.
Here are two solutions that have worked for me:
There is a Ruby gem called watir-scroll that can work.
Then
require 'watir-scroll'
browser.scroll.to browser.radio(:id, "HasRegistration_true")
If you don't want to add a gem, my co-worker suggested something that somewhat surprisingly (to me) had the same effect as the above code:
browser.radio(:id, "HasRegistration_true").focus
browser.radio(:id, "HasRegistration_true").set
In my code, ".focus" scrolled straight to the element that was previously not visible. It may work for you as well.
First of all try locating the element using XPATH:
browser.element(:xpath, "//input[#id='HasRegistration_true']").click
or
alternatively if it is a hidden element you are trying to locate then you are better off using CSS. Download firebug add-on for firefox and copy the CSS path of your element.
It should be something like:
browser.element(:css => "the CSS path you have copied from Firebug").click
One of the 2 should do it for you!!
Best of luck!
You could manipulate the html on the fly by executing some javascript to make the radio element settable. To execute javascript on a page, do something like:
#browser.execute_script("your javascript here")
I used something like the following javascript to strip the class out of a label tag which moved it out of the way of the input tag I was attempting to act on for a Chrome specific problem I had.
execute_script("$(\"label.classname\").removeClass(\"classname inline\")")
If the element is contained within a form and a div (Wrap) class I found that I had to do the following to click the 'No' radio button on the "https://quote.comparethemarket.com/Motor/Motor/AboutYourVehicle.aspx?" page:
div_list = #browser.form(:action => "AboutYourVehicle.aspx?ton_t=CTMMO&prdcls=PC&rqstyp=newmotorquote&AFFCLIE=CM01").div(:class => "inputWrap").divs(:class => "custom-radio")
And then:
div_list[1].click
Hope this solves your issue too :-)
I'm not using watir but I have the same error as you "...could not be scrolled into view ...". I tried to use watir just to solve it and didn't work for me. Then, I use an ActionBuilder (move_to with click) and the error disappeared.
My line to avoid the error is:
#driver.action.move_to(*webelement*).click.perform
I hope it will be useful for you

Selenium WebDriver and xpath: a more complicated selection

So let's say my structure looks like this at some point:
..........
<td>
[...]
<input value="abcabc">
[...]
</td>
[...]
<td></td>
[...]
<td>
<input id="booboobooboo01">
<div></div> <=======I want to click this!
</td>
.........
I need to click that div, but I need to be sure it's on the same line as the td containing the input with value="abcabc". I also know that the div I need to click (which doesn't have id or any other relevant attribute I can use) is in a td at the same level as the first td, right after the input with id CONTAINING "boo" (dynamically generated, I only know the root part of the id). td's contain nothing relevant I can use.
This is what I tried as far as xpath goes:
//input[#value='abcabc']/../td/input[contains(#id,'boo')]/following-sibling::div
//input[#value='abcabc']/..//td/input[contains(#id,'boo')]/following-sibling::div
None of them worked, of course (element cannot be found).
I want to know if there's a way of selecting that div and how.
EDIT: //input[#value='abcabc']/../../td/input[contains(#id,'boo')]/following-sibling::div is the correct way. This was suggested by the person with the accepted answer. Also note that he offered a slightly different way of doing it. See his answer for details.
Try
//input[#value='abcabc']/ancestor::tr[1]/td/input[contains(#id,'boo')]/following-sibling::div[1]
Note that //input[#value='abcabc']/.. only goes up to the parent <td>, that's why your's did not work.
Another XPath that may work, is a bit more simple:
//input[#id='booboobooboo01']/../div[1]

Clicking a button with Ruby Mechanize

I have a particularly difficult form that I am trying to click the search button and can't seem to do it. Here is the code for the form from the page source:
<input type="image" name="" src="http://images.example.com/WOKRS53B4/images/search.gif" align="absmiddle" border="0" onclick="return check_form_inputs('UA_GeneralSearch_input_form','search');" title="Search" alt="Search" class="">
I am trying to do the standard mechanize click action:
login_page = agent.click(homepage.link_with(:text => "Search"))
Is this because the button uses javascript? If so, any suggestions?
I struggled with this too, especially since my form had multiple buttons.
There are multiple ways to submit a form (with many using a 'form_with' block), but this helped me:
# get the form
form = agent.page.form_with(:name => "my-form")
# get the button you want from the form
button = form.button_with(:value => "Search")
# submit the form using that button
agent.submit(form, button)
See more info here
Also, make sure you upgrade to the latest mechanize. I was using mechanize 1.x, which was giving me "undefined method" errors for the code above.
It is not a link, it is a button. What you need to do is look for the form (for example, with form_with) and then look for the ImageButton and submit it.
button = form.button_with(value: 'Search')
form.click_button(button)

Resources