gpt4 book ai didi

excel - 使用 selenium 检查域

转载 作者:行者123 更新时间:2023-12-03 03:13:40 25 4
gpt4 key购买 nike

我正在尝试在 VBA 中使用 selenium 检查某些域这是我的尝试

Option Explicit
Sub Check_Domain()
Dim bot As New WebDriver
Dim sDomain As String

sDomain = "facebookopop.com"
bot.Start "chrome", "https://ae.godaddy.com/domainsearch/find?checkAvail=1&tmskey=&domainToCheck=" & sDomain
bot.Get "/"

Dim eleTaken As Object, eleAvailable As Object

bot.Wait 3000

On Error Resume Next
Set eleTaken = bot.FindElementByXPath("//text()[contains(.,'Domain Taken')]/ancestor::span[1]")
Set eleAvailable = bot.FindElementByXPath("//text()[contains(.,'Domain Available')]/ancestor::span[1]")
On Error GoTo 0

If Not eleTaken Is Nothing Then
Debug.Print "Not Avaialable"
ElseIf Not eleAvailable Is Nothing Then
Debug.Print "Avaialable"
Else
Debug.Print "Unknown"
End If
Stop
End Sub

代码运行缓慢,同时它并不总是给出正确的结果..如何以简单的方式检查元素是否存在并避免错误?

我不知道为什么下面的代码不起作用

Sub Check_Domain_Advanced()
Dim bot As New WebDriver
Dim sDomain As String
Dim c As Range
Dim ele As Object
Dim t
Const MAX_WAIT_SEC As Long = 10

bot.Start "chrome"

For Each c In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
If Not IsEmpty(c.Value) Then
sDomain = c.Value
bot.ExecuteScript "window.open(arguments[0])", "https://ae.godaddy.com/domainsearch/find?checkAvail=1&tmskey=&domainToCheck=" & sDomain
bot.SwitchToNextWindow

t = Timer
Do
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While bot.FindElementsByCss("span[class='domain-name-text h2']").Count = 0

Set ele = bot.FindElementByCss("span[class='domain-name-text h2']")

If ele.IsPresent Then
If InStr(ele.Text, "available") Then
c.Offset(, 1).Value = "Avaialable"
ElseIf InStr(ele.Text, "taken") Then
c.Offset(, 1).Value = "Not Avaialable"
Else
c.Offset(, 1).Value = "Unknown"
End If
End If
End If
Next c

Stop
End Sub

我需要在新选项卡中打开每个链接并检查域(可用或已占用),但我收到了元素错误(由于页面加载)对于如何改进代码以便更快地工作并避免错误有什么建议吗?

最佳答案

使用具有此字段的 API。有精确匹配 API 以及交叉销售。

完全匹配

Option Explicit
Public Sub CheckDomainAvailability()
Dim json As Object, domains(), i As Long, url As String
domains = Array("google.com", "bszadfdws.com")
url = "https://find.godaddy.com/domainsapi/v1/search/exact?q=####&key=dpp_search&pc=&ptl=&itc=dpp_absol1"
With CreateObject("MSXML2.XMLHTTP")
For i = LBound(domains) To UBound(domains)
.Open "GET", Replace$(url, "####", domains(i)), False
.send
Debug.Print JsonConverter.ParseJson(.responseText)("ExactMatchDomain")("IsAvailable")
Next
End With
End Sub
<小时/>

交叉销售以查看相关域:

https://find.godaddy.com/domainsapi/v1/crosssell/all?sld=domainNameGoesHere&key=dpp_search&pc=&ptl=&itc=dpp_absol1

然后您需要查看键 CrossSellDomains 的值而不是ExactMatchDomain

<小时/>

要求:

  1. here 下载 jsonconverter.bas 并将其添加到您的项目中
  2. VBE > 工具 > 引用 > 添加对 Microsoft Scripting Runtime 的引用
<小时/>

Selenium 版本:

使用定时循环并检查 available 的 header 内容.

Option Explicit
Public Sub CheckDomainAvailability()
Dim d As WebDriver, domains(), i As Long, t As Date, ele As Object
Const MAX_WAIT_SEC As Long = 10
domains = Array("google.com", "bszadfdws.com")
Set d = New ChromeDriver

With d
.Start "Chrome"
For i = LBound(domains) To UBound(domains)
.get "https://ae.godaddy.com/domainsearch/find?checkAvail=1&tmskey=&domainToCheck=" & domains(i)

t = Timer
Do
On Error Resume Next
Set ele = .FindElementByCss(".exact-header-tag")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If Not ele Is Nothing Then
Debug.Print domains(i) & " available = " & (InStr(LCase$(ele.text), "available") > 0)
Set ele = Nothing
End If
Next
.Quit
End With
End Sub

关于excel - 使用 selenium 检查域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55929007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com