How to click on button when <input type =Button> using Selenium - selenium

How to click on button when input type is button , I am using below code, click on button is not working?
<p>
<img class="getdata-button" style="float:right;" src="/common/images/btn-get-data.gif" id="get" onclick="document.getElementById('submitMe').click()">
<input type="button" value="Get Results" tabindex="9" id="submitMe" onclick="submitData();" style="display:none" ;="">
</p>
I have tried
driver.find_element_by_css_selector("input[#type='button']")).click()
driver.find_element_by_id("get").click()
driver.find_element_by_id("submitMe").click()
also used xpath for both still nothing

It can be done through JavaScript if that is acceptable for you:
driver.execute_script("document.getElementById('submitMe').click()")
The <img> element fires an onclick event: onclick="document.getElementById('submitMe').click()"; it locates and clicks the <input> element button below in the DOM. The Selenium click interaction can be skipped and call the same JavaScript from the <img> element which should get you the desired result.

Related

Selenium selector for checkbox pseudo-element

<label class="casllic_text" for="Checkbox">
::before
<span>Blob <a class="button link" href="abc.pdf">Terms</a> </span></label>
I want to click on checkbox.
If I use label[for="Checkbox"] as selector, webdriver clicks on Terms link which opens PDF in another tab.
How can I click on checkbox which so far only gets identified if I hover on ::before. (Used inspect element to do that)

Using Selenium to verify presence of certain css computed elements for form fields

I would need a good way to verify error icon is shown for mandatory form fields when submitted with empty input using selenium 3 webdriverjs.
Below is part of DOM trace when error is thrown if mandatory field is left blank & form is submitted.
<div class="col-md-8 col-sm-8 col-xs-8 " style="display: inherit;">
<div class="vx_floatingLabel_complex vx_floatingLabel_active vx_has-
error-with-message ">
<label for="testerAccountData_phone">Telefone</label><div
class="vx_form-control" data-label-content="Telefonnummer" style="">
<input type="tel" maxlength="20" value="" autocomplete="off"
name="/testerAccountData/phoneNumber" id="testerAccountData_phone">
</div><span class="vx_form-control-error-icon icon icon-small icon-
critical-small"></span><span></span><span></span></div></div>
I am looking at validating multiple fields in the form,
Q: how do I use selenium to check if error icon appears for the field. i'm not sure if i can use getAttribute function, as error icon seems to be part of CSS element?
=> class="vx_form-control-error-icon icon icon-small icon-critical-small">
Once you confirm that you can select the element. Selenium has the element.isDisplayed() api that can help you determine if the element is visible
http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_WebElement.html
https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/lib/webdriver.js#L2160
//untested code:
driver.findElement(By.className('vx_form-control-error-icon')).isDisplayed();

Clicking on span in between text tags using Selenium Webdriver

I want to click on <span> tag, by using 'name' element of input type radio.
for below html
<label class="mt-radio nomargin-bot">
<input name="commission_or_margin" value="margin" type="radio">
Margin
<span></span>
<label>
Thanks
Using following-sibling xpath function you can achieve this as shown below:
driver.findElement(By.xpath("//input[#name='commission_or_margin']/following-sibling::span")).click();
Let me know, whether it works for you.

Unable to click on Save button using By.className while className available in div

Unable to click on Save button using By.className while className available in
webpage code for button available
<div class="popupFooter">
<div align="center">
<input id="Preview-btn" class="btn-primary previewDetaile" type="button" value="Preview">
<input class="btn-primary validateProfile" type="button" value="Save">
<input id="clear" class="btn-primary" type="button" value="Cancel">
</div></div>
Selenium Code
driver.findElement(By.className("btn-primary validateProfile")).click();
Problem:
Unable to click on Save button
Your problem is that you search for multiple classnames which does not work with By.className. Just try
driver.findElement(By.className("validateProfile")).click();
instead. For selecting elements by multiple classnames you can find a solution here.
It's very easy with a CSS selector:
driver.findElement(
By.cssSelector(".btn-primary.validateProfile")
).click();
For a full reference:
http://www.w3schools.com/cssref/css_selectors.asp
As #Sebastian pointed out, it's probably due to the fact that By.className accepts only one class name (I think it's not by chance that they didn't name it By.classNames :) )

I can't get selenium webdriver to recognize elements on a modal that fades in

I'm testing a form. When I click on a modal, a div modal appears and the background fades out and this new modal fades in that allows you to input information. For some reason selenium won't recognize elements on this modal. Its not listed as an iframe so I'm not sure if I'm suppose to use the switch to.
the modal
<div id="addressModal-20f95ac4-8a83-4c02-862d-a42d60a74b04" class="modal hide fade in"
style="display: block;" aria-hidden="false">
text are in modal
<textarea rows="2"name="viewModel.MortgageForm.BorrowerInformationSection.Borrowers[0].Dependents.modalTextArea-addressModal-20f95ac4-8a83-4c02-862d-a42d60a74b04" id="modalTextArea-addressModal-20f95ac4-8a83-4c02-862d-a42d60a74b04" cols="20" class="span valid"></textarea>
There may be multiple elements with that same DOM signature and webdriver picked up the one which is not context of the current user view.
Solution: Since it is not an iframe, you will have to locate the element in context of the modal box container. You can try the following to locate the textarea webelement:
JQuery:
$("div[id^='addressModal']:visible").find("textarea")
WebDriver(Java):
driver.findElement(By.cssSelector,"div[id^=addressModal] textarea")