excel - 尝试填充 IE 输入框,单击提交,然后从 IE 中检索值
问题描述
我正在尝试从位于https://www.godaddy.com/domain-value-appraisal的 GoDaddy 估值服务中自动检索域名值
我已经手动完成了将近 400 个,但它非常繁琐、容易出错,而且需要很长时间。我想如果我可以在 Excel 2010 中创建一个 VBA 函数,那么我可以自动化 Internet Explorer。我将成为的机器上的 IE 版本是 IE 11。
我已经尝试了来自不同站点的几个不同的建议示例,包括 StackOverflow.com,但我没有取得任何进展。我被困在如何用我的电子表格中的域名填充文本框。所以我所做的只是硬编码一个域名进行测试。我可以编写稍后将循环通过 400 行的代码,但现在我正试图让一个工作.. 我认为 IE DOM 搞砸了一些东西。
我什至不能确定我的其余步骤是否正确编码。最新版本的代码如下。
我想自动化的有三个步骤: 第 1 步:在https://www.godaddy.com/domain-value-appraisal的输入框中输入我的电子表格中的域名 第 2 步:单击“GoValue”按钮 第 3 步:将估计值返回到我的电子表格上的一个单元格。
代码如下。感谢任何人都可以识别我做错了什么以及如何正确进行。
Sub Try3()
Dim IE As New InternetExplorer
Dim doc As HTMLDocument
Dim objDomainIn As HTMLInputElement
Dim objGoButton As HTMLButtonElement
Dim objValueOut As HTMLSpanElement
IE.Visible = True
'GO TO THE GODADDY VALUATION PAGE
IE.Navigate "https://www.godaddy.com/domain-value-appraisal"
'WAIT FOR THE PAGE TO LOAD
Do
DoEvents
Loop Until IE.LocationName = "Free Domain Value and Appraisal Tool | What is your domain worth? - - GoDaddy"
Set doc = IE.document
'I WOULD LIKE TO DO THE FOLLOWING STEPS FOR SEVERAL HUNDRED DOMAIN NAMES. BUT FOR THIS EXAMPLE, HERE IS ONE OF NAMES:
'1) insert the domain name into the "domainToCheck" textbox
'2) click the "GoValue™" button
'3) scrape the Estimated Value from the result page
'SO TRANSLATING THOSE STEPS TO VBA I TRIED:
'STEP 1)
'THE INPUT TEXTBOX ON THE FORM:
'<input name="domainToCheck" class="domain-name-input searchInput form-control" aria-label="Enter a domain name" type="text" placeholder="Enter a domain name" value="">
Set objDomainIn = doc.all.getElementsByName("domainToCheck") '<----**** I AM STUCK HERE ***
objDomainIn.Value = "MPGBooster.com"
'STEP 2)
'BUTTON THAT NEEDS TO BE AUTOMATICALLY CLICKED (I added carriage returns because it was very long):
'<button class="btn btn-primary submit "
' type="Submit"
' data-eid="gce.sales.domain-value-appraisal.domain_search_marquee.domain_search_marquee_lp_module.button"
' data-creative-slot="e6a02371-14a2-4a21-b444-4aa0ae01b3b7"
' data-creative-name="domain-value-appraisal/Domain Search Marquee LP Module/Domain Search Block/Primary CTA"
' data-promo-name="domain_search_marquee.domain_search_marquee_lp_module.button8b002669-c11d-4913-881e-fe39fce24eab"
' data-promo-id="gce.sales.domain-value-appraisal."
' data-schema="add_promotion" data-tdata="module_id,e6a02371-14a2-4a21-b444-4aa0ae01b3b7^block_id,8b002669-c11d-4913-881e-fe39fce24eab^block_path,/ComponentSettings/GoDaddy/Sales/domain-value-appraisal/Domain Search Marquee LP Module/Domain Search Block/Primary CTA^campaign_name,^redpntcn,^redpntrid,^redpntcid,^redpntsn,^redpntrc,"
' value="GoValue™">GoValue™</button>
'CLICK "GOVALUE" BUTTON TO GET DOMAINS VALUE
Set objGoButton = doc.document.getElementsByClassName("input-group-btn")
objGoButton.click
'STEP 3)
'THE RETURNED HTML SHOWING THE DOMAIN ESTIMATED VALUE
'<span class="dpp-price price"><strong>$1,426</strong></span>
Set objValueOut = doc.getElementsByClassName("dpp-price price")
MsgBox "MPGBooster.com valued at: " & objValueOut.innerText
'CLEAN UP OBJECTS
IE.Quit
Set IE = Nothing
结束子
解决方案
首先,getElementsByName 是 Document 对象的属性。其次,它返回一个从 0 开始的元素集合。因此,请尝试以下操作...
Set objDomainIn = doc.getElementsByName("domainToCheck")(0)
objDomainIn.Value = "MPGBooster.com"
或者,您可以使用 querySelector 方法...
Set objDomainIn = doc.querySelector("input[name='domainToCheck']")
objDomainIn.Value = "MPGBooster.com"
请注意,getElementsByClassName 还返回从 0 开始的元素集合。所以你需要相应地索引。
推荐阅读
- spring - 创建 bean 以连接到 Couchbase 时出现抽象方法错误
- php - 如何检查是否已使用条带支付令牌
- node.js - 使用现有 API 创建自定义 API
- c - 为什么 for 循环实际上返回一个值作为返回指令?
- php - 在 PHP 7 中使用 PHP excel 库或 python excel 库?
- javascript - 使用 SVG.JS 重叠两个 SVG 元素 - 一个响应式,一个固定式
- bash - 嵌套 for 循环内的 Bash shell 文件大小比较
- mysql - mySql 错误符号的错误:它是 Tic、Comma 还是 Quotations?
- java - 来自字符串的 CreateFont()
- python - 在 python 中读取文件时捕获错误的最佳位置是什么?