How do I get text from web brwoser into a label in vb? - vb.net

How do I get this, from my webbrowser into a label in vb?
Like.. how do do I get the "apple" into a label?
<div class="banana">
<input class="form-control" type="text" placeholder="Result" value="apple>
</div>

Try the following:
YourLabel.Text = Wb.Document.GetElementById("yourId").GetAttribute("value")
You'll have to provide the input tag in the HTML document with an id, though. If you're not in control of the HTML, take a look at GetElementsByTagName.

Related

Xpath for Submit button with precedence nodes select file and *

Query 1:
I would request you to please help me on getting xpath for submit button. Absolute path starts with Select file and *.
<div id="bg">
<label id="label" style=" font-family: Segoe UI;color:#2e2e2e; font-size:12px; float:left; padding-top:8px;">
Select File <span id="spanhide" class="red">*</span></label>
<div style="margin-left:105px;"><input type="file" name="filUploadIcon" id="filUploadIcon" class="txt-box" onchange="FileUpload_OnChange(this,event);" style="width:180px;">
<input type="submit" name="btnUploadcancel" value="" onclick="return check();" id="btnUploadcancel" title="Upload" class="upload_pop"></div>
<input name="textFileName" type="text" id="textFileName" style="display:none;">
<input type="hidden" name="hdnframeID" id="hdnframeID">
<input type="hidden" name="hdnlbl" id="hdnlbl">
</div>
Query 2:
How to write xpath to skip few nodes in between. Please help. Also let me know adding // or * in between to skip nodes.
Ex: Above HTML
//*div[#id="bg"]/skip elements before input type submit node/input [#type="submit"]
Use the below xpath to target the precedence nodes that include the label with the string "Select File" and the embedded span that contains '*'.
//div[contains(#id, 'bg')]/label[contains(text(), 'Select File')]/span[contains(text(), '*')]
Then add on the below line to return to the parent node label to the span tag.
/parent::label
Then add on the below to get to the sibling div tag of the label tag, which contains the input tag with a type of submit.
/following-sibling::div/input[#type='submit']
So the xpath in its entirety should look like this:
//div[contains(#id, 'bg')]/label[contains(text(), 'Select File')]/span[contains(text(), '*')]/parent::label/following-sibling::div/input[#type='submit']
You can use below xpath.
//div[#id='bg']//input[#name='btnUploadcancel']
Strongly suggest to go through this to learn more on xpath 1.0 which will answer all your questions in OP.

only first word from radio button selection is being returned

To get the selected option from a radio button form, I'm looking at request.vars to get the value. Following is the controller code:
def get_results():
test = request.vars['QuestionOne']
for topic in session.selectedtopics:
test = test + request.vars[topic]
return locals()
And now the code for the view:
{{extend 'layout.html'}}
<P>Please select the options that most closely approximates the actual scenario.
<br>
<form action="{{=URL('get_results')}}" method="post">
{{nameTopic =""}}
{{session.selectedtopics = list()}}
{{for topic in topics:}}
{{if nameTopic <> topic.topic.replace(" ", "_"):}}
<br><h2>{{=topic.topic}}</h2>
{{nameTopic = topic.topic.replace(" ", "_")}}
{{session.selectedtopics.append(nameTopic)}}
{{pass}}
<p><input type="radio" name={{=nameTopic}} value={{=topic.param}}>{{=topic.param}}</p>
{{pass}}
<br>
<input type="submit">
</form>
Here is the problem: I don't know the reason, but it is getting only the first word of the selected option in the radio form. For example, the option selected is "It is normal", but the var is returning only "It". Any idea why it is happening?
Thank in advance.
You need to quote the HTML attribute values:
<input type="radio" name="{{=nameTopic}}" value="{{=topic.param}}">
Without the quotes, you'll end up with final HTML like:
<input type="radio" name=some_topic value=It is normal>
The browser will interpret the value to be "It", with "is" and "normal" being invalid HTML attributes.

Read the label of a Radio Button and confirm the value with Selenium

So as the title suggests. I am looking to confirm that the value of the Radio button is correct.
The HTML is as follows:
<input type="radio" value="Coach" name="servClass" checked="">
<font face="Arial, Helvetica, sans-serif">
Economy class
<br>
<input type="radio" value="Business" name="servClass">
Business class
<br>
<input type="radio" value="First" name="servClass">
First class
</font>
The selenium bit is as follows:
String expectedServiceClass = "First class";
String actualServiceClass = driver.findElement(By.cssSelector("input[value='First']")).getText();
if (actualserviceClass.equals(expectedServiceClass)){
System.out.println("Correct Wording");
}else{
System.out.println("Oops: somethings not right with the wording");
//close Firefox
driver.close();
// exit the program explicitly
System.exit(0);
}
But when this is executed, the actualServiceClass variable doesn't contain any values i.e. null therefore the "if statement" will always print "Oops: somethings not right with the wording"
Any help???
With the current HTML code, you won't be able to confirm the value of label of Radio button as Radio button is implemented as Input tag, that is a self-closing tag and hence getText() on input will always return null. You will need a container tag like div to include the Input tag(radio button) and the label. Refer: Self-closed versus Container Tags
The problem is not with the Selenium Code, its actually due to the improper HTML snippet. Changing the HTML as below can solve this:
<font face="Arial, Helvetica, sans-serif">
<div>
<input type="radio" value="Coach" name="servClass" checked="">
Economy class
<br>
</div>
<div>
<input type="radio" value="Business" name="servClass">
Business class
<br>
</div>
<div>
<input type="radio" value="First" name="servClass">
First class
</div>
After this, just changing the Css Selector or XPath to find the div will give you value of label of Radio Button. Css Selector can be div>input[value='First']. Let me know if you are able to solve the problem.
I agree with #Manu the HTML snippet is poor but you can use javascript childNodes to get the text from the nodes
The childNodes property returns a collection of a node's child nodes, as a NodeList object.
The nodes in the collection are sorted as they appear in the source code and can be accessed by index numbers. The index starts at 0.
Use executescript to execute JavaScript in the context of the currently selected frame or window
Below is an example in java
Don't forget to add return since you need to return the value to the caller
WebElement element = driver.findElement(By.xpath("//input[#value='Coach']/following-sibling::font"));
String node_text=(String)((JavascriptExecutor)driver).executeScript("return arguments[0].childNodes[0].nodeValue",element);
System.out.println(node_text.trim());
Try the above script it will return "Economy class"
In the above script we use childnode property to get all the childnodes of
font tag <font face="Arial, Helvetica, sans-serif">
similarly you can get the other text nodes by replacing childnode index
childNodes[4]----->"Business class"
childNodes[8]------>"First class"
I tried the above code it was working fine
Hope this helps you...kindly get back if you have any queries

I want to call the element that contains "xyz" from vb.net with webbrowser

I have html like this:
<input tabindex="0" id="x-widget-2-input" value="xyz" style="width:142px;" class="GP34Q33JX GP34Q33EX" type="text">
I want to call the element that contains "xyz" from vb.net with webbrowser, anyone can help?

Paste a text to textarea in browser control in Vb.Net

How can I paste a text to the text area within a form in the browser control?
I think how i have selected is correct
browser1.Document.Forms.GetElementsByName("editform").GetElementsByName("input")
UPDATE:Here is the Html
....
<form name="editform">
<textarea name="input">
</textarea>
</form>
...
Here is an example of how it can be done based on the HTML you've provided. You must first add a reference to MSHTML via the Microsoft.mshtml. Also, I would recommed adding an id attribute to the text area then you can get to it much easier. Something along these lines.
<form name="editform">
<textarea id="myTextArea" name="input">
</textarea>
</form>
Then you can set the value property of the text area.
Dim textArea As HTMLTextAreaElement
textArea = WebBrowser1.Document.GetElementById("myTextArea").DomElement
textArea.value = "Hello World!"
Figured out it's not possible due to security reasons.