I am trying to fill out a web form in IE with data from a workbook but I am having trouble addressing the text box since it seems to lack a name.
The site is behind a login so no link, sorry. But the element presents itself as
<input type="text" placeholder=" Søg på brugernavn, referencenummer, eller e-mail" ng-model="filterSearch.search" ng-change="filterOnSearch()" ng-model-options="{ debounce: 500 }" class="ng-pristine ng-valid ng-empty ng-touched">
when inspecting it in chrome.
I have tried
ie.document.getelementsbyclassname("ng-pristine ng-valid ng-empty ng-touched").Value = "11"
but VBA throws me a run-time error '438': Object doesn't support this property or method.
Any suggestions?
It may sound confusing but anytime you are trying to pull an element by classname you need to use the
.item(0)
after that you would use something similar to
ie.document.getelementsbyclassname("ng-pristine ng-valid ng-empty ng-touched").item(0).Value = "11"
This is assuming that you are trying to fill out an input box that has the class above and that the element is the only 1 in its class or the first one.
In the event that it does not work it may not be the first element in that class and then you may need to do item(1) or item(2) etc.
Related
I am trying to use VBA to click the button: search job posting
button
The html code:
<td><br> <input type="submit" class="btn btn-primary btn-small" value="Search"></td>
<td><br>-or - Search Job Postings
I was attempting to do it by something like:
IE.document.Something.Click
Much appreciation for someone who can help me to figure out the "something"!!!
There are probably better ways to identify a button, but when I was testing this stuff for fun a long time ago checking the onclick property was the only reliable way to identify a button on the site I was interfacing with. I'm assuming you know how to reference the browser window based off what you said. I included the Microsoft HTML Object Library to get the HTMLButtonElement type.
Dim htmlElements as Variant
Dim buttonElem As HTMLButtonElement
Set htmlElements = IE.Document.getElementsByTagName("BUTTON")
For Each buttonElem In htmlElements
If InStr(1, buttonElem.onclick, "orbisApp.buildForm({action:'displayAdvancedSearch'}).submit()") > 0 Then
buttonElem.Click
End If
Next
All you need to do is simply use the form element to click the button. Your statement:
IE.document.Something.Click
Is almost spot on.
Here's the code you want to use
IE.document.Forms(0).Submit
I cannot simulate a click event on a div element. The HTML code is this one:
<div id="export" data-export="true" data-timespan="">
<button class="btn btn-default ladda-button" type="button" data-style="expand-right" data-spinner-color="#8899a6">
<span class="Icon Icon--featherDownload"></span>
<span class="ladda-label">
Exportar datos
</span>
<span class="ladda-spinner"></span></button>
<div class="hidden error server Callout Callout--danger">
<p>Ocurrió un problema al exportar tus datos, inténtalo de nuevo más tarde.</p>
</div>
</div>
In the webpage, is just a button that downloads a file when clicked. There is no URL. But after checking the code, is the div element what triggers the click event. Look the image.
Click to see image
That "g.handle" triggers a js.file form the website.
My vba code works perfect for doing other stuff in the website, but I cannot accomplish this task.
I've tried all this options:
Set div = HTMLDoc.getElementById("export")
div.FireEvent ("onclick")
div.Click
HTMLDoc.all.Item
Call HTMLDoc.parentWindow.execScript("g.handle", "JavaScript")
Nothing works. I'm kind of blocked right now and I have no idea of how to proceed. Maybe this has no solution? I have almost no idea of HTML and I have 0 idea of Javascript. But it must have a solution, because if I do it manually, i click on that DIV, wait some seconds and download my file.
Thanks in advance. Any help is welcome.
You didn't specify if the highlighted portion was the html code for the button or not, but it appears from looking at it that it isn't.
You can grab the class name for your button. The name may or may not be unique, and it's difficult to know that without seeing the entire html code, but you can set the btn variable below to the first instance of that class name. If there are multiple class names in your html code, then you will need to use a For Each...Next statement to iterate through them (unless you know the instance number off hand).
So we will go with the first instance of your class, hence the reason for the (0) appended on the end.
Sub Test()
Dim ie As New InternetExplorer, HTMLDoc As HTMLDocument, btn As Object
ie.navigate Url 'whatever this may be
' Some code to wait for your page to fully load
Set HTMLDoc = ie.document
Set btn = HTMLDoc.getElementsByClassName("btn btn-default ladda-button")(0)
btn.Click
End Sub
This is the code :
<input _ngcontent-ljn-94="" accept-user-input="true"
autocomplete="off"
class="form-control autocompleteInput ng-touched ng-dirty ng-invalid"
display-property-name="name" formcontrolname="cityId" min-chars="2"
ng2-auto-complete="" path-to-data="payload.data" placeholder="Select
city *" value-property-name="id">
I have to use sendkeys to pass value in my autocomplete city field.I tried almost various ways to pass the value by using css,xpath ,but nothing is working.Sometimes it is showing "not able to focus on element" kinds of error.Someone can please provide me the solution.
I have 2 forms that I am testing using TestStack.Seleno. Both forms have a checkbox that is mandatory. The first form has (including the checkbox) 5 fields. I can use TestStack.Seleno to create a passing test with valid data. I set the checkbox like this:
Input.TickCheckbox(f=>f.Accept,form.Accept);
On my other form which has 10 or so fields, when I try to set the checkbox to be ticked (using the same code) nothing happens. However when I try
var acceptCheckBox = Find.Element(By.Name("Accept"),new TimeSpan(0,0,0,50));
if (form.Accept)
{
acceptCheckBox.Click();
}
I get error "Element is not currently visible and so may not be interacted with"
Element is clearly visible and is not injected in using javascript.
I am using latest version of TestStack.Seleno from github.
Any ideas?
So I have managed to figure out what the issue is and have a work around, however I cannot explain why it works on the other form. The mandatory Accept field has html generated by mvc that looks like
<div>
<input class="check-box" data-val="true" data-val-mustbetrue="The Accept our field is required" data-val-required="The Accept our field is required." id="Accept" name="Accept" type="checkbox" value="true"><input name="Accept" type="hidden" value="false">
<label for="Accept">
Accept our Please accept our Terms and Conditions
</label>
<span class="field-validation-valid" data-valmsg-for="Accept" data-valmsg-replace="true"></span>
</div>
So you have the checkbox and hidden field both with id Accept, I suspect in code seleno is picking up the hidden field and setting value, so I updated my code todo
var acceptCheckBox = Find.Element(By.CssSelector("#Accept.check-box"));
acceptCheckBox.Click();
Now everything works.
Ismail
I am trying to run selenium webdriver, imitating typing something to textbox and submit it.
I have this following code:
WebElement element = null;
element = waitForElementPresent(tFByCssSelector,timeoutValue);
element.clear();
element.click();
element.sendKeys("Input String");
The code successfully type "Input String" to the textField, but when I submit the form, it says the form is empty (The form had been set to catch empty input exception).
I wonder why sendKeys does not set the value of the text field even though it has typed the wanted value into the text field.
Try to tab out of the field:
element.sendKeys("Input String");
element.sendKeys(Keys.TAB);
It may be that the field value only gets set on blur.
As I see, all looks okay in your selenium code. I believe there is some problem with your HTML form.
The form should have proper action for receiving the values. A sample form is provided below.
<form action="http://foo.com" method="post">
<input name="say" value="Hi">
<input name="to" value="Mom">
<button>Send my greetings</button>
</form>
Based on what you use in method parameter, you will get the form data in POST or GET variables.
Also you don't need to click on the element before sending keys in Selenium.