简体   繁体   English

VBA 宏如何使用 Selenium 在 chrome 上单击网站上的按钮

[英]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.我正在尝试编程以单击 chrome 网站上的一个按钮。 The name of the button is "Add to Cart".该按钮的名称是“添加到购物车”。

Please see HTML of the website: enter image description here请参阅网站的 HTML:在此处输入图像描述

And this is the VBA code:这是 VBA 代码:

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

How can I do this?我怎样才能做到这一点?

Tags are important, you can anticipate some of the events attached to certain element in DOM just by reading its tag.标签很重要,你可以通过阅读它的标签来预测某些附加到 DOM 中某个元素的事件。

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.在您的情况下,您可以直接单击按钮标签而不是单击标签span ,因为最后一个标签很少附加.click事件。

If you can provide an url to test this website I might help you better.如果你能提供一个 url 来测试这个网站,我可能会更好地帮助你。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM