Visual Basic: Set attribute of web element whitout ID - vba

HTML:
<input type="text" size="15" maxlength="79" value="" name="username">
As you can see, no ID. The HTML above is a textbox that i want to auto fill in whit my value as soon as i start the webpage whit my code.
this is what i found:
WebBrowser1.Document.Forms(0).GetElementsByTagName("username")(0).SetAttribute("value", (Text))
But whit this i get the error:
Value of '0' is not valid for 'index'. 'index' should be between 0 and -1.
Parameter name: index
What am i doing wrong?

This isn't going to find any elements:
WebBrowser1.Document.Forms(0).GetElementsByTagName("username")
"Tag name" doesn't mean the value of the name attribute, it means the name of the HTML tag itself. Like this:
WebBrowser1.Document.Forms(0).GetElementsByTagName("input")
Of course, this is likely to return multiple matched elements, so you'll need to further identify which one you want to modify. The point being that you should do some error checking to make sure that it finds anything, because trying to index an empty collection will result in an error:
WebBrowser1.Document.Forms(0).GetElementsByTagName("username")(0)
Since the collection has no elements, there is nothing at index 0.

May be u could try
Me.WebBrowser1.Document.GetElementByName("username").SetAttribute("Value", txtUsername.Text)

Related

How to use scriptAll to grab all values when the intended value is not text type

I have a page with multiple textboxes and dropdowns with values that I am trying to validate. The values in them will be dynamic in each run.
The HTML looks something like:
<input readonly="readonly" class="form-control valid" data-val="true" data="ABC" aria-invalid="false" xpath="1">
What I want to do is grab the value of "data" for each textbox. I have used scriptAll before in such a case when I was grabbing text by using innerText. However, that won't work with a regular value such as in the HTML above.
I did try one solution that worked:
driver.value(//input[#data])
However, that just grabs the first textbox value, is there a way I can combine scriptAll with driver.value? OR would I be better off doing some JS here?
Thank you in advance!
Yes, refer the docs for scriptAll(): https://github.com/karatelabs/karate/tree/master/karate-core#scriptall
Use whatever JS works to get an attribute value. Haven't tried, but this should work, you get the idea:
* def list = scriptAll('input', "_.getAttribute('data')")

Element Should Contain Text - for disabled field

I am working on automation tests using Appium and Robotframework. The keyword Element Should Contain Text seems to return empty if the input field is disabled. How to verify a disabled input field has a given value?
<input type="text" id="myId" name="myName" disabled />
I get the following error:
Element 'myId' should have contained text 'myValue' but its text was ''.
Make sure your code is correct and you are passing the correct id/classname. If this does not work please post the HTML code you are using. Adding some of the sample example which you can try out :-
If your tag is something like this below -
<input disabled="true" id='data'>
Your code should be -
WebElement webElement = driver.findElements(By.id('my-id'));
webElement.getAttribute("disabled")
or
WebElement.getAttribute("id")
For this tag -
<input id="j_idt93:j_idt93" type="text" disabled="disabled" maxlength="2000" value="Pārtraukts">
To get the value attribute -
String value = driver.findElement(By.id("j_idt93:j_idt93")).getAttribute("value");
If this does not work you may have to use the javascript executor -
String value = (String)((JavascriptExecutor) driver).executeScript("Java script query in here to return value","");
Your query should be -
return document.getElementById("j_idt93:j_idt93").getAttribute("value");
Let me know if this does not work.
Its true selenium returns empty if I assert text in disabled fields by using Element or page contains text... However we can compare the text in field by using Get Value and then comparing the field value with the value you want to assert by should be equal.
In your case what you can do is,
${valueInField} Get Value myId
should be equal ${valueInField} ${myValue}
I solved this problem.
*** Keywords ***
Should Not Be Empty
[Arguments] ${locator}
${getValueOfTextField}= Get Element Attribute ${locator} value
Should Not Be Empty ${getValueOfTextField}
I didn't get the value out, but I'm sure it's not empty.

How to get value of checkbox in capybara?

i have code html like this
<input type="checkbox" class="class-of-checkbox" value="facebook">
how to get the value (facebook) ?
You first need to uniquely find the element and then call value on it. How you find the element uniquely can be highly dependent on the structure of the rest of the page, but based on just the single element HTML you've provided (and you not actually knowing what the value already is) either of the following could be a starting place
find('.class-of-checkbox').value
find_field(type: 'checkbox', class: 'class-of-checkbox').value
If you just want to verify the value of an element with an expectation then you could do (assuming RSpec with cucumber)
expect(page).to have_field(type: 'checkbox', with: 'facebook', class: 'class-of-checkbox') # you can also pass :checked option if you want to verify it is checked - or use have_checked_field/have_unchecked_field

Using repeat.for with bound number

Consider sample below:
//edit.html
<input type="number" step="1" value.bind="number" />
<div repeat.for="num of number">${num}</div>
//edit.ts
export class Edit {
number: number = 2;
}
I expect to see 2 divs on first page load and number of divs should change when I change number in input. Instead I get error
Value for 'number' is non-repeatable
I figured it out. If you bind input field to variable, even when variable is number, it will be changed to string when changed by user. In my case, number became string once changed in input field. I used this gist to help me solve this problem:
https://gist.github.com/jdanyow/d9d8dd9df7be2dd2f59077bad3bfb399
It offers custom element and attribute for binding numbers to input fields.

Aurelia not outputting attribute with string interpolation in repeat

Is there any reason why a repeat.for binding would remove attributes from elements inside the repeater?
<div repeat.for="i of model.someArray.length">
<label>Some Array - Index ${i + 1}</label>
<input value.bind="model.someArray[i]" some-custom-attribute="someArray[${i}]"/>
</div>
and that some-custom-attribute is not being output within the repeat, but if I were to remove the string interpolation within there then it outputs fine.
== Edit ==
I have put it in a comment but just to make sure everyone is on the same page, ideally this is the output I expect:
<input value.bind="model.someArray[i]" some-custom-attribute="someArray[0]"/>
The some-custom-attribute is not an aurelia attribute, its pure HTML that a 3rd party JS library uses, so the goal here is to get the textual value of the index into the textual attribute value.
model.someArray.length is a number, not an array. You need to iterate over the array. If you do need the current index, the repeater provides the $index property for you to use.
Your code should look like this:
<div repeat.for="item of model.someArray">
<label>Some Array - Index ${$index + 1}</label>
<input value.bind="item" some-custom-attribute.bind="item"/>
</div>
To answer your original question, doing some-custom-attribute="model.someArray[${i}]" makes Aurelia think you are trying to pass a string value to the custom attribute. You can see that in the following gist: https://gist.run/?id=eed8ac8623ff4749aa5bb93c82a7b1fb I've created a custom element that just pushes whatever value it is given in to an element on the page. Note!!! Don't ever do what I'm doing here! I just did this this way so you wouldn't have to open the js console. To actually get a value passed in, you would need to use some-custom-attribute.bind="item" or (to do things how you are doing things, some-custom-attribute.bind="someArray[i]"