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
2 Answers
Reset to default 2Elements loaded via AJAX are not immediately available in the DOM. Try these fixes:
- Explicit Wait: Use
chrome.Wait
to pause until elements appear. - Refresh Elements: Re-run
FindElementsByTag
after the table loads. - 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
版权声明:本文标题:vba selenium - unable to access website elements after ajax loads data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745020257a2638089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论