Tomas
Tomas

Reputation: 21

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

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?

Upvotes: 0

Views: 1010

Answers (1)

Josias Maestre
Josias Maestre

Reputation: 127

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)

Upvotes: 1

Related Questions