longvh211 / Chromium-Automation-with-CDP-for-VBA

A method to directly automate Chromium-based web browsers, such as Chrome, Edge, and Firefox, using VBA for Office applications by following the Chrome DevTools Protocol framework.
MIT License
45 stars 6 forks source link

Fill value to field not working #21

Open dseah opened 1 year ago

dseah commented 1 year ago

I have this issue where I opened edge browser and navigate to google.com to fill in value in the search field but the field remains empty. objBrowser.navigate ("https://google com"), isInteractive objBrowser.getElementByQuery("[name='q']").value = "automate edge vba"

Here is the error log: varID 29817 = document.querySelector ("[name='q']") | Returned: null varID 29817.value="automate edge vba" | Returned: TypeError: Cannot set properties of null

This happened when I worked on my company laptop, however it works fine on my personal laptop. What do you suggest that I can do to troubleshoot?

longvh211 commented 1 year ago

It returned null meaning the element was not found under the query string. Perhaps in different setup, the element attribute changes.

dseah commented 1 year ago

Is there a way to find out what it changes to?

longvh211 commented 1 year ago

Simply inspect the HTML element as well as its structures carefully again on different environments and you will perhaps find a clue.

dseah commented 1 year ago

One thing I noted that the page loaded quite slow, could it be that script runs before the page finished loading? I see there's a wait function, but I'm not too sure how do I use it . Tried adding objBrowser.wait(iscomplete) before or after the objBrowser.navigate but doesn't seem to work too. How can I ensure that page is fully loaded before the script runs?

longvh211 commented 1 year ago

Test your theory by adding a custom sleep for a specific time with objBrowser.sleep. If it works with the sleep to input the search box then your theory is correct.

dseah commented 1 year ago

I added objBrowser.sleep(2) after the navigate, it works now. Thanks!

longvh211 commented 1 year ago

I see. In that case there is a dynamic wait function that is even better for your case. You can use the .onExist method of an element to wait until it appears. Something like this: objBrowser.getElementByQuery(...).on Exist.value = (...)

dseah commented 1 year ago

.onExist works perfectly! Thanks!