0

Theres a website I need to scrape some data from using vba selenium chrome. Website loads a table with ajax, and I can wait for the table to fully load, however, I cannot access the loaded elements. There are input fields and buttons I need to interact with, but I cannot access any of it, other than elements that loaded initially?

I used the code below just to confirm

For Each t In chrome.FindElementsByTag("input")
    debug.print t.tagName
Next

There are 2 "input" fields on the website and it only finds the first one, not the other one loaded by ajax, obviously same goes for everything else. How can I access these elements?

5
  • Difficult to know the reason for what you're seeing without a URL or relevant source HTML... Commented Mar 5 at 16:54
  • I understand, unfortunately I cant provide url/html as this is a private website with a lot of sensitive data Commented Mar 6 at 9:43
  • @Daniel so you may prepare a minimal example of the html structure without the sensitive data. What is different between these two inputs? Commented Mar 6 at 11:35
  • FindElements grabs what is available (no waits for the element like FindElement) which is why your example below likely works. You need to add a wait, for the ajax to execute, and the 2nd element to appear. Commented Mar 7 at 3:37
  • Thats not it, I dont need to add any more wait, element is loaded while Im searching for it. I have a custom function that waits for ajax to execute, also for testing I added brute force wait, and even tried with a stop then resume script manually. Commented Mar 11 at 10:08

2 Answers 2

2

Elements loaded via AJAX are not immediately available in the DOM. Try these fixes:

  1. Explicit Wait: Use chrome.Wait to pause until elements appear.
  2. Refresh Elements: Re-run FindElementsByTag after the table loads.
  3. Use ExecuteScript: Inject JavaScript to fetch elements directly.

Try this:

Dim inputs As Object
Set inputs = chrome.ExecuteScript("return document.querySelectorAll('input');")
For Each t In inputs
    Debug.Print t.tagName
Next

This ensures you get all inputs, including dynamically loaded ones.

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately this didnt help - I can brute force wait until the page loads before resuming my script - all elements exist on the page, but theyre not in DOM or page source
Also i noticed that chrome.FindElementByTag("body").text actually returns all text from the page, table included, yet i cannot access these elements - what am I missing?
0

I have found the solution, but if I'm completely honest, I'm not sure why it works. Its as simlpe as:

Set searchbar = chrome.FindElementByXPath("//input[@id='jpkm2']")

Does anyone know why this method allows me to access these elements, while looping through all elements on the page doesnt find it, and .FindElementById throws "NoSuchElementFound" instead?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.