Xpath select parent element using inner text - xpath

<div>
<input type="checkbox" data-bind="checked: isSelected">
Select this box
</input>
</div>
Hi I am attempting to select the checkbox using the innertext within the xpath however no luck so far.
For example I know the following does not work: //input[contains(text(), ' Select this box')]
Any suggestions as to the correct syntax please? Note, it needs to include the innertext.
Thanks

Your code works (tested with XpathpatherizerNPP):
//input[contains(text(),"Select this box")]

Related

Not able to click a dropdown field which is designed in the DOJO Html using selenium

I am not able to click an arrow drop down filed in my application using selenium web driver.
I tried lot of XPath using class name and relative XPath
This is the code used for the problem
<span class="dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport"
data-dojo-attach-point="iconNode"></span>
Please add some more information from your HTML i just add some text and xpaths are like that
<span class="dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport"
data-dojo-attach-point="iconNode">test</span>
Xpaths are:
//span[#class='dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport']
or
//span[#data-dojo-attach-point='iconNode']
or
//span[#data-dojo-attach-point='iconNode' and #class='dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport']
Add some more informaton if you have any concern
dojo combo are basically <input type= "text">, once u click on it or type the first letter of the option you want to select, a <div> is attached to the html body which has following structure:-
<div resultname="option name" resultvalue="option value" class="dojoComboBoxItem dojoComboBoxItemEven ">Option Value</div>
now there are 3 steps to select from dojo
identify the input text
type the first few letters of the option you want to select
create dynamic xpath to select the option
the code goes as followes
String optionName = "Option You Want to Select";
WebElement dojoBox = driver.findElement(By.xpath("<provide the xpath here>"));
dojoBox.sendKeys(optionName.substring(0,2));
driver.findElement(By.xpath("//*[#id='page-home']/span/div[#resultvalue='" + optionName + "']")).click();
if you are not sure about the dynamic xpath structure, then manually select the option, and inspect the div you have added, generally it should have the similar structure.

How to get value from a placeholder using xpath

All of the elements are dynamic. I can see only Placeholder which is unique from the following html:-
<input
id="ext-gen1617"
type="text"
size="20"
class="x-form-field x-form-text x-form-focus"
autocomplete="off"
aria-invalid="false"
placeholder="Gender"
data-errorqtip=""
role="textbox"
aria-describedby="combobox-1166-errorEl"
aria-required="true"
style="width: 78px;"
/>
I need to get the value displayed in
placeholder="Gender".
I tried using
//input[#placeholder='Gender']
But my webdriver script failed to identify it.
Can anyone please help me out with possible solution to it?
String s=driver.findElement(By.xpath("//input[#placeholder='Gender']")).getAttribute("placeholder");
System.out.println(s);
To get an attribute for a filed, you can use the .getAttribute() method.
I assume you are dealing with (front end)script generated web elements, then you must need to embrace lean way of thinking. Don't try to pull out a web element by it's property alone. If you are not getting them try to build a xpath from its parent or siblings.
say, the HTML goes like this,
<div id="somestatic id">
<div id="xyz">
<input name="dynamic one"/>
</div>
</div>
Then you can build a xpath as ,
//*[#id='staticID']/div/input
for the HTML,
<div id="staticID"></div>
<input name="dynamic one"/>
the xpath is ,
//*[#id='staticID']/following-sibling::input
similarly there are n number of option available. Give them a try
Try
driver.findElement(
By.cssSelector("input[id*='ext-gen']")
).getAttribute("placeholder")
Let me know is the above statement is working or not.
The best way is to find the element with CSS selector in this case -
input[placeholder='Gender'], which can easily find the element.

Generic Xpath in selenium

I'm new to Selenium Webdriver. I have been using Firebug & Firepath to generate xpath (copy pasting the given xpath) for the web elements, but I am facing problems when using such xpaths(Such as "Xpath cannot be evaluated into an web elemnt").
Please help me with the below example of xpath of a Webelement to create a flexible & generic xpath:
<input type="text" maxlength="15" length="40" value="" name="ST_ACK_NUM"/>
Like the people say in the comments it is better to create a more relative path to your elements. Maybe you can post some more input so the XPath can be created more efficiently.
To get the input with a absolute XPath you can do:
//input[#name='ST_ACK_NUM']
Above XPath will search the complete source to all <input> elements where attribute name equals the value ST_ACK_NUM
When you look at your source maybe you can adjust the XPath and add more dependencies. For example if your input looks like:
<div class="DivClass">
<form name="FormName">
<input type="text" maxlength="15" length="40" value="" name="ST_ACK_NUM"/>
</form>
</div>
You could use a XPath like:
//div[#class='DivClass']/form[#name='FormName']/input[#name='ST_ACK_NUM']
This will also find the <input> element, but with a lot more dependencies.

Select preceding-sibilings of element that contains child in xpath

I have code that looks like this
<div id="content">
<p>text</p>
<p>text1</p>
<p>text3</p>
<p><iframe></iframe></p>
<p>text4</p>
<p>text5</p>
</div>
I need to select all "p" elements before "p" that contains child "iframe", I tried something like this
//div[#id='content']//p[iframe]/preceding-sibiling::p
but with no luck.
I am stuck here for a few days so any help would be great.
Your XPath logic was actually fine! You just have a few typos it appears.
//div[#id='content']/p[iframe]/preceding-sibiling::p
You also don't need the extra / after the div block. Here's the full working xpath:
//div[#id='content']/p[iframe]/preceding-sibling::p

Check boxes within the select box

How to render checkbox within the select box.
Can i use like below code:
<form:select path="test" items="testlist">
<input type="checkbox name="test"/>
</form:select>
Thanks in Advance.
Raj
Ah, it would be nice if it were so easy. No, you can't do this. A select box must contain option tags, and option tags contain text. HTML just doesn't support a multi-select box with checkboxes.
That doesn't mean you can't do it. Typical workarounds involve javascript and jQuery; here's an example. Here's one that doesn't use jquery.
Use:
<input type="checkbox" name="test" checked="checked"/>

Resources