instead of xpath what can i select from the below code - selenium

Please help me to find locator in this case as id is dynamic and it changes every time when I refresh the page.
type="text" also I will not be able to use because, for the next fields, eveything is same from the down code except, the label changes from the "first name" to the "last name" and so on.
So I should be selecting something in terms of the first name which is mentioned in the label tag below.
Please anyone can help me in this case.
<input class="md-input-element ng-valid ng-dirty ng-touched" id="md-
input-4-input" spellcheck="false" type="text">
<!--template bindings={}-->
<label class="md-input-placeholder md-float md-empty" for="md-input-
4-input">First name * <!--template bindings={}--></label>

You can use preceding-sibling or following-sibling feature from xpath. As per code mentioned above your xpath should be like this:
//input[#type='text'][following-sibling::label[contains(text(),'First name')]]
But I think in html label should be first then input, if that is case please try below one:
//input[#type='text'][preceding-sibling::label[contains(text(),'First name')]]

you can get the css selector from the class (can't do by className if it has spaces in the class name so use css, put a "." at the beginning and replace any spaces with a ".")
#FindBy(css = ".md-input-element.ng-valid.ng-dirty.ng-touched")
private WebElement elementMDInputTouched;
#FindBy(css = ".md-input-placeholder.md-float.md-empty")
private WebElement elementMDInputEmpty;

Related

How to validate Check box if xpaths are same in case of selected and unselected

M unable to validate checkbox, if it's selected or not
because both the HTML are same
I tried isSelected(), but it's not working
Below is the HTML code for both selected and unselected
1) Selected
<label class="c-account-access-panel__checkbox " for="23336" data-js-checkbox-label="">
<input id="23336" class="c-account-access-panel__checkbox-input" type="checkbox"
data-label-for-value-missing="Please select at least one account from the options below" data-form-field-validation-on-grid=""
required="" checked="" data-js-checkbox="" value="DE29973399" name="payer"/>
<div class="c-account-access-panel__checkbox-symbol"/>
2) Unselected
<label class="c-account-access-panel__checkbox " for="23336" data-js-checkbox-label="">
<input id="23336" class="c-account-access-panel__checkbox-input" type="checkbox"
data-label-for-value-missing="Please select at least one account from the options below" data-form-field-validation-on-grid=""
required="" checked="" data-js-checkbox="" value="DE29973399" name="payer"/>
<div class="c-account-access-panel__checkbox-symbol"/>
Thanks in advance!
As per the Java Docs isSelected() method determines whether the element is selected or not. This operation only applies to <input> elements such as checkboxes, options within a <select> tag and radio buttons.
To validate if the desired checkbox is selected or not you can use the following code block:
boolean checkboxSelected = driver.findElement(By.xpath("//input[#class='c-account-access-panel__checkbox-input' and #name='payer']")).isSelected();
If isSelected() is not working for you. Then , you can use JavascriptExecutor to do your task. Following JS statements shall let you know the state of target checkbox.
document.getElementById("23336").click();
document.getElementById("23336").checked;
The checked method returns true or false depending on the checkbox state.
You can validate using getAttributemethod.
First select the webElement using any of the unique locator,
WebElement checkbox=driver.findElement(By.xpath(".//input[#type='checkbox']"));
If the checkbox is selected, then checkbox.getAttribute("checked")will give the result as true else, it will give the result as null. So, you can add the condition using checkbox.getAttribute("checked")
Use xpath expression like: (//div [#id='23336')[1] or (//div [#id='23336')[2] to make them into unique element then do .isselected ()

Xpath not finding element (parent/ancestor)

<div class="slds-show" data-aura-rendered-by="10155:0">
<div class="footer" data-aura-rendered-by="10156:0">
<div class="slds-grid slds-grid--align-end slds-m-top--large" data-aura-rendered-by="10157:0">
<div class="slds-show" data-aura-rendered-by="10158:0">
<button class="slds-button slds-button--neutral slds-m-left--small" data-aura-rendered-by="10159:0">Cancel</button>
<button class="slds-button slds-button--neutral slds-m-left--small" data-aura-rendered-by="10161:0">Save & New</button>
<button class="slds-button slds-button--brand slds-m-left--small" data-aura-rendered-by="10163:0">Save</button>
</div>
</div>
</div>
This is part of page, on which I will have to click on Save button.
Button is not unique and I need to find it throu class attribute from first div (slds-show), or
Can somebody tell me, why this xpath is not finding this element?
//button[parent::div[#class='slds-show'][#class='slds-button slds-button--brand slds-m-left--small']]
I've also try with ancestor, text instead of class and results is the same. Element is not found via Firefox console
To click on Save button once finding it through class attribute from first div (slds-show) you can use a much simpler and effective xpath as follows :
//div[#class='slds-show']/button[#class='slds-button slds-button--brand slds-m-left--small']
Note : The class attribute slds-button--brand is unique for the Save button.
Try to update your expression as below:
//button[parent::div[#class='slds-show'] and #class='slds-button slds-button--brand slds-m-left--small']
Note that predicate [#class='slds-button slds-button--brand slds-m-left--small'] in your XPath intend to test #class value of parent div, but not target button
You can try the following xpaths.
//*[#class="slds-show"]/button[text()="Save"]
or
//*[class="slds-show"]/button[#class="slds-button slds-button--brand slds-m-left--small"]
An xpath can easily get too complex, you can also try something like this:
//button[text()='Cancel']
//button[text()='Save & New']
//button[text()='Save']
These will return the exact buttons you need. If you're looking for a specific ancestor, include it in your xpath:
//div[#class="slds-show"]//button[text()='Save & New']

How to Select a checbox by its corresponding label name

I am relatively new to xpath and I'm having an issue with Selecting a checkbox.
The following expression works fine
:xpath=//div/label[#for='chkCat2']
But I need to select a checkbox based on its label name (Music in this case)
Unfortunately, this -
xpath=.//div[label[contains(text(),'Music')]
is clicking on the label, not on the checkbox.
How is it possible to do that? I also tried this one but its not able to locate the element:
xpath=.//div[label[contains(text(),'Music')]/preceding-sibling::label]
That's the sample of the code:
<input name="ctl00$cphContent$ThreeMedia$categoriesRepeater$ctl01$ctl00"
data-bind="attr:{'id': 'chkCat' + id, 'data-id': id}, checked: selected" id="chkCat2" data-id="2" type="checkbox">
<label data-bind="attr:{'for': 'chkCat' + id}" for="chkCat2" class=" firepath-matching-node"></label>
<label class="chkbox-content margin-bottom-0" data-bind="text: name">Music</label>
Thanks for the help
But I need to select a checkbox based on its label name (Music in this case)
Assuming div is the parent element of the provided checkbox element, try using below xpath :-
.//div[normalize-space(.)='Music']/input
or more specific
.//div[normalize-space(.)='Music']/input[#type='checkbox']
one more
.//div[label[text() = 'Music']]/input
Try this below mentioned xpath
//label[text()= 'Music']/..//preceding-sibling::input[#id='chkCat2']
Explanation of xpath:- Use text method along with <label> tag and move ahead with <input> tag using preceding-sibling keyword.

Find element by XPath isn't working in selenium

I'm trying to find a element in selenium with this XPATH /html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input.
I copied it from inspect element, and copy xpath. I saw that some persons with the same problem use "*" character but I don't know where I should to use it.
this the html code
<input type="text" data-bind="value: CorpItem.Name, valueUpdate: 'afterkeydown'"
class="form-control" placeholder="Enter ..." required="required">
Here is my code on Selenium
IWebElement corpName = driver.FindElement(By.CssSelector("/html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input"))
Try this XPath
//input[#type='text'][#class='form-control']
In your code you used XPath and gave selector as CSS. Please verify.
Using your XPath, your code must be
IWebElement corpName = driver.FindElement(By.XPath("/html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input"))
If it didn't work, try
IWebElement corpName = driver.FindElement(By.XPath("//input[#type='text'][#class='form-control']"));

How to check if radio button is selected or not using Selenium WebDriver?

<div class="my_account_module_content">
<h3 class="my_account_module_content_title">
Mi Dirección 1
<br>
<span>Predeterminada</span>
<div class="selectCard_left">
<input id="17390233" class="default_shipping_address" type="radio" name="address" checked="true">
<span>Seleccionar como tarjeta predeterminada</span>
</div>
this is the HTML code
If radio button selected is true then print the class span value?
please help me..
In Java, this would do it:
if(driver.findElement(By.id("17390233")).isSelected()){
System.out.println(driver.findElement(By.xpath("//input[#id='17390233']/following-sibling::span[1]")).getText());
}
If the radio button is selected, then the text will show. If you want to use the text somewhere, I suggest you put it in a string instead:
String spanText = driver.findElement(By.xpath("//input[#id='17390233']/following-sibling::span[1]")).getText();
Hope this answers your question.
EDIT: Here is an update of other ways to try.
If the className default_shipping_address is unique (e.g. not used anywhere else on the page), you may try locating the element by className:
if(driver.findElement(By.className("default_shipping_address")).isSelected()){
System.out.println(driver.findElement(By.xpath("//input[#class='default_shipping_address']/following-sibling::span[1]")).getText());
}
If that class is not unique, maybe the DIV's className selectCard_left is?
if(driver.findElement(By.className("selectCard_left"))){
System.out.println(driver.findElement(By.xpath("//div[#class='selectCard_left']/span[1]")).getText());
}
If none of the classNames are unique, a complete xpath expression is required. If you still are unable to get that text, I refer to reading up on how to use xpath: http://www.w3schools.com/XPath/xpath_syntax.asp
I hope that you find this information useful.