excel - 尝试使用 Selenium + Excel VBA 从 Chrome 浏览器中的网站抓取代码

标签 excel vba selenium xpath css-selectors

我需要一些有关在 Excel (2013) VBE 中使用 Selenium Basic ChromeDriver (v 75.0.3770.140) 从 Chrome (v 75.0.3770.142) 网站抓取文本的指导。

这是 HTML:

<div ng-if="!ShippingDetails.isShippingCreatedOutsideSH" ng-bind="ShippingDetails.code" class="">3333333</div>

我需要提取的部分是“3333333”

我对 Selenium 类型库还是全新的,到目前为止,我还是笨拙地尝试了这个新手尝试:

Dim SHN As String
SHN = obj.FindElementByXPath("//div[@ng-if='!ShippingDetails.isShippingCreatedOutsideSH']/ng-bind[@'ShippingDetails.code']").Text
MsgBox SHN

最佳答案

要打印文本3333333,您可以使用以下任一 Locator Strategies :

  • 使用CSS_SELECTOR:

    • 使用.Text:

      Debug.Print .FindElementByCss("div[ng-if$='isShippingCreatedOutsideSH'][ng-bind^='ShippingDetails']").Text
      
    • 使用.Attribute("innerHTML"):

      Debug.Print .FindElementByCss("div[ng-if$='isShippingCreatedOutsideSH'][ng-bind^='ShippingDetails']").Attribute("innerHTML") 
      
  • 使用XPATH:

    • 使用.Text:

      Debug.Print .FindElementByXPath("//div[contains(@ng-if, 'isShippingCreatedOutsideSH') and starts-with(@ng-bind, 'ShippingDetails')]").Text
      
    • 使用.Attribute("innerHTML"):

      Debug.Print .FindElementByXPath("//div[contains(@ng-if, 'isShippingCreatedOutsideSH') and starts-with(@ng-bind, 'ShippingDetails')]").Attribute("innerHTML") 
      

You can find a relevant discussion in Vba selenium code to get data from Li class

关于excel - 尝试使用 Selenium + Excel VBA 从 Chrome 浏览器中的网站抓取代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57135802/

相关文章:

excel - 间接,列引用和行引用位于不同的单元格中

VBA全局类变量

excel - 在for循环中复制偏移范围

python - 除了一页上的一个 CSS 列表外,不抓取所有请求的数据

google-chrome - 在第二个监视器屏幕中运行的 Selenium Chrome 窗口窃取焦点

java - TakesScreenshot 无法解析为 Selenium WebDriver 中的类型

python - xlsxwriter 布隆伯格公式

Excel/VBA 基本函数问题

excel - 为什么Excel在更改工作表的Worksheet_Change事件上崩溃?

Excel VBA,将鼠标移动到不同的单元格