VBA automation can't get innerText (innerHTML, value) - vba

So, i've gotten innertext many times. This time, it's simply not working. Tried many alternatives.
First of All
I'm using Selenium on VBA to use chromedriver.
The source website is "[http://www.fazenda.df.gov.br/area.cfm?id_area=1536]" This website operates on a Iframe. After half an hour trying to get the inner text from it, i figured that the link in its code works just as fine: "[http://publica.agnet.fazenda.df.gov.br/FichaImovel/#/FichaCadastral]". This last one seems to operate on someting call "Angular".
The menu with the innertext i required is a dropdown one. As a dummy value, you can use 50868748. The dropdown text that it generates is composed by 15 lines and one table. The info that i need is on the 15th line, "Nome do Responsável", or in the column 2 line 2 on the table.
I managed to place the code and generate the dropdown list, but i can't manage to get the inner text.
Here is my code:
Dim iptu As String
Dim driver As New ChromeDriver
With driver
.Start
iptu = 50868748 'dummy
.Get "http://publica.agnet.fazenda.df.gov.br/FichaImovel/#/FichaCadastral"
.FindElementById("inscricaoImovel").Clear
.FindElementById("inscricaoImovel").SendKeys iptu
.FindElementsById("btnCadastro")(2).Click
.Wait (300)
'until here, everything works fine
Dim label As Object
Dim name As String
Set label = .FindElementsByClass("ng-binding")(91)
'as you can see, i end up using this class(91) to get the value on the table, but i can get from the 15th line too
name = label.getAttribute("innerText") 'Error is here
'i've tried .innerText, .innerHTML and others. The error is always "object does not accept property or method" Error 438.
end with
The chrome driver is up to date. Also, the page source does not cointain the info i need.
Anyone can help me out?

To get InnerText of the element, You need to call Text method
Set label = .FindElementsByClass("ng-binding")(91)
name = label.Text
To get inner Html,
name = label.Attribute("innerHTML")

Sometimes I have found that I have to execute javascript to get what I'm after. You can try
name=driver.ExecuteScript("document.getElementsByClassName('ng-binding')[91].innerText;")

Related

How to select dropdown lists with no id

I've been researching this for the last couple days but haven't found a solution yet. Any help would be greatly appreciated.
I'm using VBA to put a push button on an Excel spreadsheet which will open an Internet Explorer window, log people into a particular site, search for a particular "case" (webform) on the site, fill in the values the way the user specified on the sheet, and then submit the form. Everything works except the form fields that are drop down lists. When I run it, it fills in the values I want in those fields but it seems like it's not actually selecting them from the list, just overwriting what's displayed there, because when it clicks submit an error window pops up saying to fill in those drop down fields.
From my research I've seen various solutions for how to search a drop down list and set it to the selected index once it's located the text I want, but my problem is that I haven't had luck selecting the drop down list at all. If you look at the screenshot below, that shows one of the fields as it looks in Internet Explorer's dev tools:
The highlighted portion is the id number I use to paste in my text (what I'm doing right now that isn't working) but the line below it with class ns-dropdown seems like it might be what I really want to access, but that line doesn't have an id.
Here's the relevant portion of my code:
Dim IE As Object
Dim IEForm As HTMLDocument
Set IEForm = IE.Document
With IEForm
.getElementById("custevent_crc_code_fs").innerText = Sheet1.Cells(9, 3) 'CRC Code
.getElementById("inpt_status4").innerText = Sheet1.Cells(8, 3) 'Status
.getElementById("inpt_custevent95").innerText = Sheet1.Cells(11, 3) 'Department
.getElementById("inpt_custevent426").innerText = Sheet1.Cells(10, 3) 'Case Origin
.getElementById("inpt_custevent447").innerText = Sheet1.Cells(6, 3) 'Created By
.getElementById("inpt_custevent1188").innerText = Sheet1.Cells(7, 3) 'Contact Reason
.getElementById("btn_multibutton_submitter").Click
End With
Let me know if you need any more information. I've been wrestling with this for a while and am trying not to flood you with too much extraneous info about things I've tried.

How to read text from a specific line, compare with another line and display

I've searched and found similar topics but none that really answer my question, or perhaps it's the fact that I'm a beginner and I don't fully understand yet.
What I'm trying to do is use a ComboBox and a TextBox. Once the ComboBox selection changes, the TextBox will load more information about the item in the ComboBox. This is for my company, trying to store canned messages with a title.
So I want to have a title for these messages in a ComboBox drop-down, and once a title is chosen, the message is displayed in the TextBox. Right now, I've got the message creation, which is also part of my program, appending to two different text files, since I'm not sure yet on how to append data from two controls onto the same line and concatenate a Tab or other delimiter.
So my question is, what method should I be going about doing this? The part I am REALLY not understanding is how to get the line number value from the selected item in the TextBox, they load just fine using the following line:
savedmsgComboBox.Items.AddRange(File.ReadAllLines("MessageListTitles.txt"))
So how do I get which line this selection is on, and how do I correspond that line with the message I need to display in the TextBox?
This will only work if both arrays have the same amount of lines. Load both files into arrays.
'declare the arrays
Private msgTitles() As String
Private msgList() As String
'in load event fill them
msgTitles = File.ReadAllLines("MessageListTitles.txt")
msgList = File.ReadAllLines("MessageList.txt")
'load the combobox
savedmsgComboBox.Items.AddRange(msgTitles)
'get the items by the index
Dim msgListItem As String = msgList(savedmsgComboBox.SelectedIndex)

Visual Basic Web Browser change form when certain webpage content detected

I am trying to make a form on VB.NET that when a certain webpage is loaded into the web browser (in this case its a .txt file with a string of numbers). It will look for those numbers and if it finds them it will close that form and open another. I have tried to do it many times and my latest attempts came out with this code. Any help would be appreciated.
Dim passcontents As String
passcontents = WebBrowser.DocumentText = "test.com/test.txt"
If WebBrowser.DocumentText = "test.com/test.txt" And passcontents Then
ActualGen.Show()
Me.Close()
Else
End If
There are a couple of basic problems with the code you've posted.
1) Are you trying to test passcontents as a boolean or a string? You've defined it as a String, but then you are trying to assign the equality of two other strings to it, as you would a Boolean.
Try setting Option Strict On at the top of your code - it gives a few more helpful pointers to check in this case.
2) The If... Then condition is checking the same data as passcontents (may) already be providing. Are you just trying to check that the correct page is loaded, or do you want to check for certain page contents as well?
You appear to be getting mixed up between the browser's document URL and the loaded document contents.

VBA and DOM to fill out input box, in a frame (code here)

I have made references to the MSHTML library and MS Internet Controls setup, and I am trying to fill out a form using a variable. The main page consists of 3 frames, and the "top" frame is where my form is located. The code below will work, if I talk to the frame directly, with the form in the frame being called "NavPage". and the Cnum being the name of the element i want to add a value to, then click a button to retrieve the number
Dim HTMLDoc2 As MSHTML.HTMLDocument
Set HTMLDoc2 = Browser.document
HTMLDoc2.forms("NavPage").CNum.Value = "12345" 'will change this to become a variable
HTMLDoc2.forms("Navpage").GetCase.Click
Question is, how do I reference to the first top frame using DOM, because right now, it doesnt work, and it spits "Object variable or With block variable not set" at me. Thanks for any insight, as theres not much to be found concerning VB about this
You should be able to use this to reference the document in the first frame:
Set HTMLDoc2 = Browser.document.frames(0).document

How to manipulate user selected text using webdriver?

Lets say i have the following snippet in my web page:
<p> This is some text </p>
I want WebDriver to select "some" in this text, as if the user selected it. How should i do this? I know how to get the <p>-element:
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p"));
System.out.println(p.getText());
The println prints "This is some text".
I tried sending keys to the element, and that used to work(in selenium 2.0b), but i'm using selenium 2.6.0 now, and it stopped working:
editable.sendKeys(Keys.chord(Keys.SHIFT, Keys.LEFT));
Does anyone have ideas? I'm using the FirefoxDriver.
I did this once for Firefox using Javascript. Basically I used the range object in Firefox to select the text. Change the start and end range index based on what you want to select. This would not work in IE, because selecting range is conceptually different in IE. I don't have the IE code handy but since you are concerned about FF, you could give this a shot.
Let me know if you interested in IE text range.
String script = "var range = document.createRange();" +
"var start = document.getElementById('idofthedivthatcontainstext');" +
"var textNode = start.getElementsByTagName('p')[0].firstChild;" +
"range.setStart(textNode, 8);" +
"range.setEnd(textNode, 13);" +
"window.getSelection().addRange(range);";
((JavascriptExecutor)driver).executeScript(script);
You are trying to select the contents of the p tag by drag select right I am not sure if that is objectively possible to be done by the user as your are suggesting.. Selenium now tries to mock the exact action that a user can perform on a browser. thus you just cant send a shift and left select key on the p tag and expect it to select unlike a textbox where it is very much possible but you might probably have to click on the text box first to get it into focus.
Here is what I would suggest to achieve this, not sure if it will work.
a) send the left click on the p tag
b) hold the shift key
c) drag the mouse to the end of the p tag.
Hope that helps.
Use the .Text property of the IWebElement
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p").Text).ToString();
editable.Replace("This is ", "").Replace(" text.");
System.out.println(p.getText());