Home > Blockchain >  VBA macro how to click on button on website on chrome, using Selenium
VBA macro how to click on button on website on chrome, using Selenium

Time:08-01

I am trying to program to click on one button on website on chrome. The name of the button is "Add to Cart".

Please see HTML of the website: enter image description here

And this is the VBA code:

CD.FindElementByCss("div.addToCartButtons clearAfter > button blue right s-addToCart > span.Add to Cart").Click

How can I do this?

CodePudding user response:

Tags are important, you can anticipate some of the events attached to certain element in DOM just by reading its tag.

In your case you can click on the button tag directly instead of clicking the tag span, since this last one rarely has a .click event attached to it.

If you can provide an url to test this website I might help you better. This are the possible approaches:

1) Advanced: 
   a. Spaces in class are change for dots
   b. Requires to understand tag's meaning and relative position of elements in DOM
        'Example
        'Clicking first button in div using only tag ("button" is a tag)
         CD.FindElementByCss("div.addToCartButtons.clearAfter > button").Click
 2) Intermedium:
    a. Use only first Class and ignore all others after first space
    b. Requires to understand what an elemment.child is
        'Example:
        'Clicking first child of "div" (Everything inside "div" is a child)
         CD.FindElementByCss("div.addToCartButtons > button:nth-child(1)").Click
'3) Easiest:
    a. Double quotes (") inside querySelector are changed to single quote (')
    b. Requires to use Copy JS path in DevTools (Ctrl   Shif   I)
        'Example:
        'Clicking with Javascript in your website through ExecuteScript 
        strTemp = "document.querySelector('div.addToCartButtons clearAfter > button blue right s-addToCart').click"
         CD.ExecuteScript (strTemp)
  • Related