i want to xpath "field_name" before <label for="street" string="Address"/>
but i can xpath just label :'(
<xpath expr="//label[#for='street']" position="after">
<field name="field_name"/>
</xpath
Some one please help me and thank you for you time to reading my word
try this, You can give position like after, before, attributes
<xpath expr="//label[#string='Address']" position="before">
<field name="field_name"/>
</xpath>
OR You can use another way like
<label for="street" position="before">
<field name="field_name"/>
<label for="field_name"/>
</label>
For this kinds of problems (where you don't have a name reference), you have to play with paths. Let's say that you have your label in an structure like this:
<form>
<sheet>
<div>
<label string='street'/>
<label string='postal code'/>
<div>
</sheet>
</form>
Now, you need to reach the firs label, so you can do something like this:
<xpath expr='//sheet/div/label[1]' position="after">
<field name="field_name"/>
</xpath>
Explanation:
With the expression, we're calling, in first place, the relative path of the first sheet we have (// are for relative path), so we can reach it anywhere in the code, second the absolute path of that div (/ is for absolute path) and finally, since we have to labels, but we want the second one, we use [1] to get it. This exact path is not always necessary, I am just suggesting it because usually we have multiples divs, but just one sheet, that's why I took it to be the root, but of course you have to adapt this to your own structure. For more information about xpath, you can read https://www.w3schools.com/xml/xpath_syntax.asp
Related
I have the following structure (it's just for sample). In protractor, I am getting the top element by id. However, the other elements do not have id's. I need to get the "label" element that contains the text '20'. Is there an easy way in protractor to select the element with a specific tag that contains a specific text from all the descendants of a parent element?
<pc-selector _... id="Number1">
<div ...></div>
<div ...>
<div ...>
<check-box _...>
<div _ngcontent-c25="" ...>
<label _ngcontent-c25="">
<input _ngcontent-c25="" type="checkbox">
<span _ngcontent-c25="" class="m-checkbox__marker"></span>
20 More text to follow</label>
</div>
</check-box>
</div>
</div>
</pc-selector>
I could't find anythitng, so I have tried with xpath, but protractor complains that my xpath is invalid:
parentElement = element(by.id('Number1'));
return parentElement.element(by.xpath(".//label[contains(text(),'20'))]"));
Any ideas?
You have an additional bracket in your [contains(text(),'20'))] which is likely causing you issue but there are multiple other ways this can be achieved using a single XPath or chaining other locators.
The process is that you must find the div with the correct id first and then locate the label that is a child of it.
//Xpath
element(by.xpath("//pc-selector[#id='Number1']//label[contains(text(),'20')]"));
//Chained CSS
element(by.id('Number1')).element(by.cssContainingText('label','20'));
You also may be interested to learn about xpath axes which can allow us to do very dynamic selection.
You can use the direct xpath to access the label.
element(by.xpath("//*[#id='Number1']//label"));
I'm trying to parse an existing document and modify it by wrapping a div around some existing form elements.
HTML form looks a bit like this:
<form>
<label for="username">Username:</label>
<input name="username" type="text" />
<label for="password">Password:</label>
<input name="password" type="password" />
</form>
I can parse the document OK with Nokogiri and i'm aware of the wrap method but i'm struggling to grasp how to select both the label and input tags in one go and then wrap a div around these. So the result I am looking for is:
<form>
<div class="form-group">
<label for="username">Username:</label>
<input name="username" type="text" />
</div>
<div class="form-group">
<label for="password">Password:</label>
<input name="password" type="password" />
</div>
</form>
I have tried various XPaths / CSS selectors and can create a nodeset of just labels/inputs or all of the elements of the whole form. Is there any way to achieve this modification?
A single XPath expression can only return a single collection of nodes, so in order to achieve what you want you will need to make several queries, one for each label – input pair.
You can select an individual pair with something like this, assuming the markup is well behaved (i.e each input has a label before it):
//label[1] | //label[1]/following-sibling::input[1]
This will select the first label and the following input. However you want to select all such pairs. One way would be to first select all the label nodes, and then for each label select it and the following input.
labels = doc.xpath("//form/label")
labels.each do |l|
nodes = l.xpath(". | ./following-sibling::input[1]")
# nodes now contains a label-input pair...
end
I don’t think the wrap method will work to add a div element as an ancestor to each pair, as it will add the element to each member of the nodeset. You will probably have to move them manually, something like
labels = doc.xpath("//form/label")
labels.each do |l|
# Select this node and its neighbour.
nodes = l.xpath(". | ./following-sibling::input[1]")
# Create the new element, and add it before the label.
div = Nokogiri::XML::Node.new('div', l.document)
l.before(div)
# Move each of the pair onto this new element.
nodes.each do |n|
div.add_child(n)
end
end
Note that this method doesn’t move any text nodes, so you may find the whitespace of your document changes a bit.
I have a requirement to conditionally validate the format or required=true/false based another fields in a beanmapper configuration.
Example:
<stream name="stream1" format="fixedlength" minlength="101" maxlength="101">
<record name="record1" class="a.b.c.SomeClassName">
<field name="updateType" required="true" length="1" regex="A|C" />
<field name="firstName" required=? length="50" format=?/>
<field name="lastName" required=? length="50" format=?/>
</record>
</stream>
Condition scenario 1: for updateType="A", I need firstName to have not null ( not all spaces as this is a fixed length format record) and for updateType="C", firstName must be all blanks.
Condition scenario 2: for firstName=some value, lastName must also have some value (non blank)
How can I achieve this in Beanio? I am using spring-batch 3x for my job execution.
I could not get any BeanIO utility for this, out of the box. The way I am handling this now is performing those conditional validations in the spring batch processor.
I am creating q site using Conreate 5 CMS.
I am creating a new block and I have to use AXMLS to create a database table.
I have been able to create a basic table but I would like to extend it to add an check constraint. Below is the code I have written so far.
<?xml version="1.0"?>
<schema version="0.3">
<table name="btAddVehicle">
<field name="bID" type="I">
<key />
<unsigned />
</field>
<field name="title" type="C" size="100">
<NOTNULL />
</field>
<field name="imgLocation" type="X"></field>
<field name="year" type="I" size="4"></field>
<field name="desciption" type="X"></field>
</table>
</schema>
so for the line I was intending to that size="4" would restrict the number of values to 4. eg 1990,1999 but this didn'st work
I have read this http://phplens.com/lens/adodb/docs-datadict.htm#foreignkey
but it seems unclear
It says
CONSTRAINTS Additional constraints defined at the end of the field definition."
SO would it be something like
<field name="year" type="I" constraint="check([year] LIKE REPLICATE ('[0-9]', 4)) "></field>
Or do I need to add an at the end of this code
`<sql>
add some kind of alter table to add the constraint
AlTER tblTable etc....
</sql>`
The size=4 only restrict it to a number between 0 and 9999. So no, that won't work.
I don't know how to use CONSTRAINTS either, but I have never needed that. Normally I use Concrete5's validation helper to validate values before saving them to the database. This also makes it a lot easier to get it printed out to the user in your view, if needed, using the validation error helper
Hope this helps you...
i have a text box in my web application,Where i need to give input. I am trying to find the xpath of the text box. the following error is thrown.
Unable to locate element: {"method":"xpath","selector":"
HTML code:
<div class="input">
<input id="firstName" class="long" type="text" maxlength="50" value="" name="firstName
I want the xpath for firstName textbox.
//input[#type='text']
And this for generally targeting a text input (what I was after)
Try this one:
//input[#id='firstName']
Explanation:
// search on all levels
input for element nodes with the name of "input"
[#id='firstName'] having an attribute (#) with the name of "id" and a value of "firstName"
at least 3 simple ways to get this:
1)Driver.FindElement(By.XPath("//input[#id='firstName']"));
2)Driver.FindElement(By.Id("firstName"));
3)Driver.FindElement(By.CssSelector("#firstName"));
//*[text()[contains(.,'firstName')]]
finding by text would always work.