首页 > 解决方案 > 如何将光标聚焦到带有预插入斜杠(_ _/_ _/_ _ _ _)的文本框的起始位置,用于 VBA 中的日期字段

问题描述

我有一个文本框,其中已经插入了斜杠,例如 (_ / / _ _ _) 。我遇到的问题是光标被放置在文本框的末尾而不是日期,因为日期是从文本框的起始位置输入的。我传递的值采用这种格式(“mmddyyyy”)。

Dim BirthDate As String
BirthDate = Format(Range(cell).Value, "mmddyyyy")

bot.FindElementByXPath("//*[@id='DateOfBirth']").SendKeys BirthDate 
' when I run this line the cursor goes to the last position in the textbox.

如何将光标的焦点设置到起始位置。我编写的脚本是使用 selenium webdriver、chromedriver 在 VBA 自动化中编写的。

标签: excelvbaselenium-chromedriver

解决方案


我假设你已经声明了类似的东西

Dim bot As New Selenium.ChromeDriver

所以也在那里声明

Dim Keys As New Selenium.Keys

你可以使用类似的东西

Dim BirthDate As String
BirthDate = Format(Range(cell).Value, "mmddyyyy")

bot.FindElementByXPath("//*[@id='DateOfBirth']").SendKeys Keys.Home
bot.FindElementByXPath("//*[@id='DateOfBirth']").SendKeys BirthDate 

推荐阅读