【问题标题】:Getting error while searcing for an element through .IsElementPresent Selenium in Excel VBA在 Excel VBA 中通过 .IsElementPresent Selenium 搜索元素时出错
【发布时间】:2018-04-07 15:47:19
【问题描述】:

我在 VBA Excel 中使用“.IsElementPresent”功能时遇到了问题。

其实我想运行一个查询,在 Chrome 浏览器的加载页面中,如果 XYZ 元素不会找到或存在,那么执行 1 否则执行 2

但我无法设置正确的语法。

每次我遇到错误时

找不到对象

这是我的一段代码:

If bot.IsElementPresent(By.XPath("//[@id='main']/footer/div[1]/div[2]/div/div[2]"), 1000) Then
    Application.SendKeys ("~")
    bot.wait 2000
    ThisWorkbook.Sheets("Sheet1").Cells(rng, 3).Value = "YES"
    rng = rng + 1
    wait = 2
Else
    GoTo bypass
End If

在这里我尝试了不同的任务,但仍然出现错误。谁能告诉我为什么我会得到这个?

请帮我解决这个问题。谢谢你。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我找到了解决方案。来自 GitHub 论坛的一个人给了我一个解决问题的想法。

    代码如下:

    Sub iselementpresenttest()
    
        Dim bot As New WebDriver
        
        bot.Start "chrome", "https://www.google.com"
        bot.Window.Maximize
        
        bot.Get "/"
        
        If bot.FindElementsByXPath("//*[@id='tsf']/div[2]/div[3]/center/input[2]").Count > 0 Then
            bot.Quit
            MsgBox "Yes, Available"
        Else
            bot.Quit
            MsgBox "Not Available"
        End If
        
    End Sub
    

    我正在尝试使用上面的代码是,

    1. 打开浏览器
    2. 最大化浏览器窗口
    3. 跑到 Google.com 地址
    4. 想知道天气“手气不错”按钮是否存在
    5. 我收到带有“是的,可用”的消息框

    它对我有用。让我尝试更多任务!建议我更多的可能性。

    还有,

    我发现下面这段代码在完成这项任务时非常重要。

    .Count > 0

    和“FindElementByXPath”的复数形式“FindElementsByXpath”代替!

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 2015-10-17
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多