首页 > 解决方案 > 如何在不可见元素上设置日期

问题描述

我正在尝试在 VBA excel 宏中使用 selenium for chrome 从网站读取数据。

https://www.eex.com/en/market-data/power/futures

我需要有关意大利期货的数据,因此我打开了页面:

driver.Get "https://www.eex.com/en/market-data/power/futures#%7B%22snippetpicker%22%3A%22EEX%20Italian%20Power%20Futures%22%7D"

driver.Wait 1000
driver.FindElementByXPath("//input[@class='btn bordered uo_cookie_btn_type_1']").Click

接下来,我必须设置日期,但该元素不可见:

driver.FindElementByXPath("//input[@class='mv-input-box']").Clear
driver.FindElementByXPath("//input[@class='mv-input-box']").SendKeys ("2020-06-19")

这不起作用:

Run-time error-12
Element is not currently interactable

我不知道如何点击图标来设置日期:

<div class="mv-button-base mv-button-image mv-stack-block mv-hyperlink-button mv-calendar-icon"><div>
</div><svg viewBox="0 0 22 17" class=""><path d="M16,4H14V0h2ZM13,1H9V3h4Zm4,0V3h3V15H2V3H5V1H0V17H22V1ZM8,0H6V4H8ZM8,9H4V5H8ZM7,6H5V8H7Zm6,3H9V5h4ZM12,6H10V8h2Zm6,3H14V5h4ZM17,6H15V8h2ZM4,10H8v4H4Zm1,3H7V11H5Zm8,1H9V10h4Zm-1-3H10v2h2Zm6,3H14V10h4Zm-1-3H15v2h2Z"></path></svg></div>

标签: selenium

解决方案


我通过添加 Post.IsDisplayed 解决了这个问题:

       For Each Post In driver.FindElementsByXPath("//input[@class = 'mv-input-box']")
        If Post.Value = Year(Date) & "-" & Format(Month(Date), "00") & "-" & Format(Day(Date), "00") And Post.IsDisplayed Then
            
            Post.Clear
            Post.SendKeys ("2021-25-01")
            
            Post.Click
            driver.Wait 2000
            Exit For
       End If
    Next Post

推荐阅读