【问题标题】:How to close popup using Selenium VBA如何使用 Selenium VBA 关闭弹出窗口
【发布时间】:2020-06-22 22:59:14
【问题描述】:

我一直在尝试关闭打开链接时出现的弹出窗口。

我尝试使用 Xpath 在弹出窗口上单击关闭按钮,但代码不断打开一个新页面,显然单击了错误的链接左右。

这是我正在尝试的代码。

Public Sub scrapeCIL2()
    Dim bot As New WebDriver, btn As Object, i As Long, pageCount As Long

    Dim wins As Variant



    bot.Start "chrome", "https://duproprio.com/en/search/list?search=true&regions%5B0%5D=6&is_for_sale=1&with_builders=1&parent=1&pageNumber=2&sort=-published_at"
    bot.Get "/"
    pageCount = bot.FindElementsByClass("pagination__item").Count
    Application.Wait Now + TimeValue("00:00:02")


    'To click the close button of the popup
    bot.FindElementByXPath("//*[@id='react-component-InfoSessionsPopup']/div/div/div[1]").Click


    bot.Quit

End Sub

任何使用 Selenium VBA 关闭此弹出窗口的帮助将不胜感激

【问题讨论】:

    标签: excel vba selenium web-scraping


    【解决方案1】:

    要关闭打开link 时出现的弹出窗口,您可以使用以下任一Locator Strategies

    • 使用FindElementByCss()

      bot.FindElementByCss("div.webinar-popup__close").Click
      
    • 使用FindElementByXPath()

      bot.FindElementByXPath("//div[@class='webinar-popup__close']").Click
      

    【讨论】:

    • 不是关闭弹出窗口,而是打开一个新标签
    【解决方案2】:

    我会等到 Selenium 完全加载 javascript,然后使用宏或 RPA 工具(如 Approbotic)通过 X/Y 屏幕坐标单击关闭按钮。像这样的东西应该适合你:

    Public Sub scrapeCIL2()
        Dim bot As New WebDriver, btn As Object, i As Long, pageCount As Long
        Set x = CreateObject("AppRobotic.API")
        Dim wins As Variant
    
    
    
        bot.Start "chrome", "https://duproprio.com/en/search/list?search=true&regions%5B0%5D=6&is_for_sale=1&with_builders=1&parent=1&pageNumber=2&sort=-published_at"
        bot.Get "/"
        pageCount = bot.FindElementsByClass("pagination__item").Count
        Application.Wait Now + TimeValue("00:00:02")
    
    
        'To click the close button of the popup
        'bot.FindElementByXPath("//*[@id='react-component-InfoSessionsPopup']/div/div/div[1]").Click
        'Move the mouse to X/Y coordinates after looking up popup coordinates with UI Item Explorer
        Call x.MoveCursor(500,500) :
        'Wait one second
        x.Wait(1000) :
        'Click the popup close button
        x.MouseLeftClick() :
    
        bot.Quit
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 2019-10-22
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多