admin管理员组

文章数量:1410717

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?

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?

Share Improve this question asked Mar 5 at 16:38 DanielDaniel 9546 silver badges12 bronze badges 5
  • Difficult to know the reason for what you're seeing without a URL or relevant source HTML... – Tim Williams 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 – Daniel 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? – Shrotter 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. – DMart 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. – Daniel Commented Mar 11 at 10:08
Add a comment  | 

2 Answers 2

Reset to default 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.

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?

本文标签: vba seleniumunable to access website elements after ajax loads dataStack Overflow