【问题标题】:How to click the date picker selenium VBA?如何单击日期选择器硒 VBA?
【发布时间】:2018-10-08 06:58:31
【问题描述】:

我想使用 selenium VBA 从Yahoo Finance KOSPI COmposite Index 下载一些数据。

我在单击日期选择器箭头以使迷你窗口选择结束日期为今天时遇到了困难。我尝试在chrome中通过selenium IDE记录marco,但是当我单击时间段的箭头以使日期选择器可见时,IDE没有记录步骤。

下面是我在 VBA 中的代码。

Public Function seleniumKorea(bot As WebDriver)
    Dim url As String
    url = "https://finance.yahoo.com/quote/%5EKS11/history?period1=1484018309&period2=1515554309&interval=1d&filter=history&frequency=1d"
    bot.Start "chrome", url
    bot.Get "/"

    'Not sure how to add date picker here        
    bot.FindElementByName("endDate").Clear
    bot.FindElementByName("endDate").SendKeys (Date)
    bot.FindElementByXPath("(.//*[normalize-space(text()) and normalize-space(.)='End Date'])[1]/following::button[1]").Click

    Application.Wait (Now + TimeValue("0:01:00"))

    bot.FindElementByXPath("(.//*[normalize-space(text()) and normalize-space(.)='As of'])[1]/following::div[4]").Click
    Application.Wait (Now + TimeValue("0:01:00"))
    bot.FindElementByXPath("(.//*[normalize-space(text()) and normalize-space(.)='Currency in KRW'])[1]/following::span[2]").Click

    Application.Wait (Now + TimeValue("0:01:00"))
End Function    

我尝试使用 ByXPath 获取 svg 类但失败了。

提前致谢。

【问题讨论】:

    标签: vba selenium selenium-webdriver web-scraping


    【解决方案1】:

    如果需要,我将使用以下提交 OATH 同意并将日期选择器滚动到视口中

    Option Explicit
    Public Sub DatePicking()
        Dim d As WebDriver
        Set d = New ChromeDriver
        Const URL = "https://finance.yahoo.com/quote/%5EKS11/history?period1=1484018309&period2=1515554309&interval=1d&filter=history&frequency=1d/"
    
        With d
            .get URL
            If .Title = "Yahoo is now part of Oath" Then
                .FindElementByCss("form").submit
            End If
    
            With .FindElementByCss("[data-test='date-picker-full-range']")
                .ScrollIntoView
                .Click
            End With
            With .FindElementByCss("[name=startDate]")
                .Clear
                .SendKeys "05/10/2017"
            End With
    
            With .FindElementByCss("[name=endDate]")
                .Clear
                .SendKeys "05/10/2017"
            End With
            Stop                                     '<==Delete me later
            .Quit
        End With
    End Sub
    

    【讨论】:

    • 谢谢。您的方法效果很好,但我需要在输入正确的日期后单击“完成”按钮。我试过你上面提到的方法。With .FindElementByCss("[name=Done]") .Click End With但失败了。请问如何点击这个数据选择器上的完成按钮。
    • 恐怕我没有收到这条消息,所以我的回答正是您所要求的。对不起。但是 SIM 已经为您提供了那部分。
    【解决方案2】:

    看看这个。它应该服务于目的。我使用xpath 解决了这个问题。

    Sub CustomizeDate()
        Const Url$ = "https://finance.yahoo.com/quote/%5EKS11/history?period1=1484018309&period2=1515554309&interval=1d&filter=history&frequency=1d"
        Dim driver As New ChromeDriver
    
        With driver
            .get Url
            .FindElementByXPath("//input[@data-test='date-picker-full-range']", timeout:=5000).Click
            .FindElementByXPath("//input[@name='startDate']").Clear.SendKeys ("01/05/2017")
            .FindElementByXPath("//input[@name='endDate']").Clear.SendKeys ("01/08/2017")
            .FindElementByXPath("//button/span[.='Done']", timeout:=5000).Click
            .FindElementByXPath("//button/span[.='Apply']", timeout:=5000).Click
        End With
    End Sub
    

    不要使用硬编码延迟来显示任何项目。请改用Explicit Wait。脚本将等待 5000,这意味着该项目可用 5 秒。

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      相关资源
      最近更新 更多