following code:
<label>
<input class="class1" type="checkbox" checked="checked"/>
First text
</label>
<label>
<input class="class2" type="checkbox" checked="checked"/>
Second text
</label>
<label>
<input class="class3" type="checkbox" checked="checked"/>
Third text
</label>
What I want to do is to get specific label element by containing text. I was trying:
//label[text()='First text']
and
//label[contains(text(),'First text')]
but it doesnt work.
Please, advise!
Thanks! :)
//label[text()[contains(., 'First text')]]
Your attempt
//label[contains(text(),'First text')]
does not work because the <label> in
<label>[
]<input class="class1" type="checkbox" checked="checked"/>[
First text
]</label>
has two text nodes: an empty one containing nothing but a line break, right before the input, and a non-empty one after the <input>. I've outlined them with square brackets above.
A call like contains(node-set, string) forces a conversion of the first argument to contains to string.
Converting a node-set to string gives you the text content of the first node of the set. (Try it out, string(label) will give you 'First text' with a bunch of whitespace, no matter how many labels there are.)
And in your case, that's the empty text node, so contains(text(),'First text') will never succeed.
Therefore you must test the text nodes individually, and that's done by nesting predicates like shown above.
Related
Take for example this two tags:
<input type="submit" value="aValue" />
and
<input type="submit" value=aValue />
I'm trying to get a True/False if a tag has an attribute without quotes. Using Beautifoul Soup 4 find method it prints me always aValue between quotes, event if I give second tag as input.
There is a way to catch tags with attribute without quotes?
I am trying to select the value from a dropdown box using vba, the code block for the dropdown box is as follows
<input type="text" id="form_autocomplete_input-1542902425322" list="form_autocomplete_suggestions-1542902425322" placeholder="Search keyword or select filter" role="combobox" aria-expanded="false" autocomplete="off" autocorrect="off" autocapitalize="off" aria-autocomplete="list" aria-owns="form_autocomplete_suggestions-1542902425322 form_autocomplete_selection-1542902425322">
If the value of form_autocomplete_suggestions-1542902425322 was static I would use .Document.getElementById("form_autocomplete_suggestions-1542902425322").Value = "Role: Student" however this seems to be a randomly generated numerical value.
I have had a look and it seems I cannot simply add a wildcard in such as .Document.getElementById("form_autocomplete_suggestions-*").Value = "Role: Student"
And as its randomly generated and such a long number it cannot loop through an array of values. so I am unsure on how to solve this issue.
You can use css attribute equals value selector syntax with the ^ operator to say starts with a certain substring. You could also use * instead, which means contains.
[id^='form_autocomplete_input-']
VBA:
ie.document.querySelector("[id^='form_autocomplete_input-']")
You might also use:
[placeholder='Search keyword or select filter']
Which would be:
ie.document.querySelector("[placeholder='Search keyword or select filter']")
As you indicate this needs to be selected you may need:
ie.document.querySelector("[id^='form_autocomplete_input-']").Selected = True
Reference:
CSS attribute selectors
I'm trying to use digestive-functors to parse a form with a variable number of dynamically generated inputs, something like this:
<form>
<input type="hidden" name="object-id" value="123">
<input type="hidden" name="object-id" value="43">
<input type="hidden" name="object-id" value="467">
</form>
But am unsure on how to do this. I see there's a listOf function, but it looks like it requires the input names to have an index in them and be created all at once, which I don't want, since these inputs are being populated dynamically.
The haskell type is something like:
data Form = Form { objectIds :: [Int] }
Any ideas?
I have this code
<label>
<input type="radio" checked="checked" value="fOejPdlZIx83HA" name="btnRad">
Test1
</label>
<label>
<input type="radio" checked="checked" value="fdsaf4waff4sssd" name="btnRad">
Test2
</label>
<label>
<input type="radio" checked="checked" value="fg43fasd43wsat4" name="btnRad">
Test3
</label>
I wish to access the radio button depending on the label text via xpath
I already tried multiple thing:
//input[#name='btnRad]']/following::*[contains(text(),'Test3')]
//label[text()='Test3']/input[#name='btnRad']
//*[contains(text(),'Test3')]
Even the last one return me nothing, so xpath think that "Test3" is not the text of the label... anyone have an idea how to do this?
Your expression is failing because your label has more that one text node: an empty string before the input, and Test3. The way you're using contains means it will only check the first text node, ie empty string.
Two ways of solving this:
eliminating the empty strings with normalize-space():
//*[contains(text()[normalize-space()], 'Test3')]
querying each text():
//*[text()[contains(.,'Test3')]]
For a more detailed explanation, see How to search for content in XPath in multiline text using Python?.
This works also //label[contains(.,'Test3')]/input .
Basically, I have made a form which allows you to input 2 numbers, and when you press the 'Add' button, the program writes the answer onto the screen, the only thing is when the answer is written, it appears on a separate page. How do I get it to write to the same page, below is the HTML code:
<form type="twoNum" method="get">
<input type="float" placeholder="Enter first number here..." name="num1" id="n1"/>
<input type="float" placeholder="Enter second number here..." name="num2" id="n2"/>
<input type="button" value="Add" name="sndfunct" onClick="twoNum(this.form);"/>
</form>
Below is the Javascript code:
function twoNum(form)
{
var num1 = form.num1.value;
var num2 = form.num2.value;
var intNum1 = parseFloat(num1);
var intNum2 = parseFloat(num2);
document.writeln(intNum1 + intNum2);
}
Please note that using document.write() is considered bad practice. E.g. see the warning on the W3C web site.
Furthermore, you can’t use it to edit a closed document. document.write() can only be used while the document is being loaded.
In order to do what you want, you should have a <span id="foo"></span> somewhere in your document, and then do:
document.getElementById("foo").textContent = intNum1 + intNum2;
This will insert your number inside your span element. Actually, it replaces the content of the (previously empty) span element.
Edit: Of course, it can be any kind of element. I used a span element just for the example.
You can write <p id="answer"></p> in the form. Then create var answer=intNum1 + intNum2 and after that instead document.writeln(intNum1 + intNum2) write document.getElementById("answer").innerHTML=answer!