fetch html data from website into excel vba - excel

Object does not support this property or method.
Error occur on this line:
For Each ele In objIE.document.getElementsByClassName("table info-table").getElementsByTagName("tr")

getElementsByClassName() returns a collection of matching elements (even if there's only one match), so you need something like (e.g.):
For Each ele In objIE.document.getElementsByClassName( _
"table info-table")(0).getElementsByTagName("tr")
which will loop over the tr elements in the first table with a matching class name.
If you need a different table you'll need to adjust the (0)

Related

(Python) Selenium list objects are not callable when looping through them (autocomplete don't work either)

I'm fairly new to Python and Selenium.
I'm trying to gather elements from a webpage using Python and Selenium in VS Code.
I've already done similar things in other webpages so I can confirm the setups and the drivers all work fine.
Here is the code which I'll try to explain line by line.
'// Creating an empty Array of Names'
Names = []
'// Finding and Saving the Table im interested in'
Table = driver.find_element_by_id("pokedex")
'// Finding and Saving in a list all the elements with a particular class name'
NameCells = Table.find_elements_by_class_name("cell-name")
'// Looping through the List'
for NameCell in NameCells:
'// If it finds a child element with a particular class...'
if NameCell.find_elements(By.CLASS_NAME, "text-muted"):
'// ... append it in the array once transformed into text'
Names.append(NameCell.find_element(By.CLASS_NAME, "text-muted").text)
'// ... else...'
else:
'// ... append an element with another class into the array once transformed into text.'
Names.append(NameCell.find_element(By.CLASS_NAME, "ent-name").text)
'// .. and print the array.'
print(Names)
The problem is that while I can use functions like "find_element" in the second and third line of code... I can't use it in the for loop, in the fifth line of code.
VS Code doesn't even show me the expected functions after digiting the ".".
I tried to complete it myself hoping it worked but of course it didn't.
Why does it happen?
Why can't I use WebElements functions at times?
I'm noticing it's happening mainly on Lists of objects rather than single objects.

Problems to extract links with webscraping

I want to extract the links of the toys listed in this webpage:
https://cebra.com.ar/category/73/Juego-de-Construccion.html
I have an entire procedure (I don´t copye here because it´s very long and complicated), in which in some part I have the following code that doesn´t work:
Cells(erow, 1) = html.getElementsByTagName("a").href
Any idea to solve this?
Thanks a lot!
getElementsByTagName returns a collection and indeed you would need to index into it to get a particular element.
However, you don't want all a tags. That is inefficient and you would need an additional test to limit to those of interest. You want specifically the links for products so use an attribute = value css selector to get those:
Dim links As Object, i As Long
Set links = html.querySelectorAll("[href^=product]")
For i = 0 to links.Length - 1
ActiveSheet.Cells(erow + i, 1) = links.item(i).href
Next
This:
[href^=product]
looks for href attributes whose value starts with, ^, product.
If you look at the page html you can see each of your target links begins with that substring
The function getElementsByTagName() of the object HTMLDocument returns a list, but you're trying to access the property .href of one object as if it was a single object.
You should replace this:
Cells(erow, 1) = html.getElementsByTagName("a").href
with this
Cells(erow, 1) = html.getElementsByTagName("a")[yourIndex].href
... where yourIndex is a number representing the index of your list (0, 1,... n).
Of course you'll have to find the correct rule to get the right a element at the right place, as just getting all the elements of the document with tag a retrieves 278 elements in your page (including all the page headers, footers and other things I don't really think you need):

How to set up prefixes on saved search for multiple choices in netsuite?

I have a multiple select field that allows multiple selections of colors. I created a formula that would append a prefix of "color-" to each selected list, but it only appends it to the the beginning of the field. I'm not sure how I can split the field results up for the formula to where I can get it showing up for all results.
CASE WHEN {custitemtag_color1} is NULL
THEN ''
ELSE 'color-'||{custitemtag_color1}
END
Results with multiple selections show: color-Black,Lime Green,White
Expected Results need to show: color-Black,color-Lime Green,color-White
What's happening is that NetSuite returns "Black,Lime Green,White" as a single result for the multi-selection, then you're prepending "color-" to that text returned. To work around it within your saved search, you could simply replace any instances of the comma (",") with ",color-":
CASE WHEN {custitemtag_color1} is NULL THEN '' ELSE 'color-'|| REPLACE({custitemtag_color1}, ',', ',color-') END

Error using TransferSpreadsheet to import an Excel spreadsheet

I have a routine that helps me locate a the row of word in a standardized worksheet.
Based on the location of the word - for example, I search in column 'A'
It finds the word on row 7.
I now know that I can use the range A8:M14 as the data I want to import into my table, so I created a function 'GETBASELINE' that would just return that string - "A8:M14"
So now I have a table called tbl_TEMP_Import with these fields
BASELINE|OCT|NOV|DEC|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP
I call it like so:
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tbl_TEMP_Import", strPath, False, GetBASELINE(strPath, strSheet)
I get Error 2391 Field 'F1' doesn't exist in destination tbl_TEMP_Import
If I change it to 'True' for 'has field names'
I get Error 3270 'Property Not Found'
I wish I could get better debugging. Doesn't seem too complex to accomplish this.
Answer with 'True' for 'has field names':
You have to give the table the field heading as well... so the code for GETBASELINE must equal "A7:M8" ... (I'm not sure where 14 came from)
A7:M7 are Field names
A8:M8 are 1st set of values
Answer with 'False' for 'has Field names':
You must change the field names, because the call brings in temp field names, F1,F2 and so on
Manually you can change the names. It is programmable as well.
In any event, once you call the script one way, be careful not to 'mix' them with each other! Delete one before you call the next

an efficient way acess a range in a user defined type?

I've got a user defined type with about 5000 entries. I would like to select a range of the data in about 1,000 entry blocks and use it as an array. Is there a way to do this without looping?
Something like
MyArray = MyType(1:1000).property
rather than
for i = 1 to 1000
MyArray(i) = MyType(i).property
Next i
Thanks!
No, there is no way to convert a collection of elements into an array without looping or special accessor methods for the type - let alone convert a common property from a collection of elements into an array.
The only objects that support anything like this are Range objects, for which you can convert certain properties of an array of cells (range) into an array using:
MyArray = Range("A1:A1000").Value
Again, nothing like this is available for other types unless the programmer goes through the trouble of defining the behaviour - and even if they did define it, odds are the method would likely include iterating over the elements anyways, just within the type class.

Resources