使用硒检查域 [英] Check domain using selenium

查看:45
本文介绍了使用硒检查域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to check for some domains using selenium in VBA Here's my try

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

代码运行缓慢,但同时又无法始终为我提供正确的结果..如何才能以一种简单的方式检查元素的存在并避免错误?

The code runs slowly and at the same time it doesn't give me correct results all the time .. How can I check for the existence of an element in an easy way and avoid errors?

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

I don't know why the following code doesn't work

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

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

I need to open each link in a new tab and check for the domain (available or taken) but I got errors as for the element (because of the page loads) Any suggestions how to improve the code so as to work faster and to avoid errors?

推荐答案

使用具有此字段的API.有完全匹配的API和交叉销售.

Use the API which has a field for this. There is an exact match API as well as a cross sell.

完全匹配

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


交叉销售以查看相关领域:


Cross sell to look at related domains:

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

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

You would then need to look at the value for key CrossSellDomains instead of ExactMatchDomain

要求:

  1. 此处下载并添加到项目jsonconverter.bas
  2. VBE>工具>参考>添加对Microsoft脚本运行时的参考


硒版本:

使用了定时循环并检查标头的内容是否可用.

Used timed loop and check contents of header for available.

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

这篇关于使用硒检查域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆