Home > Back-end >  How to click on a textbox with date format and type in value using selenium with VBA?
How to click on a textbox with date format and type in value using selenium with VBA?

Time:01-19

I am writing an Excel macro using Selenium basic. I have a textbox with to enter date from a worksheet cell value. The problem is that when I try to input the date value the position of the cursor start on the middle which is the Month value, I need the cursor to start at the leftmost part of the textbox.

I tried without using "driver.FindElementById().Click" but it does not input any value at all. But with "driver.FindElementById().Click" this is what happens, refer to the image below. enter image description here

in case picture can't be uploaded... _ _ _ _/| / _ error in output 20/22 output should be 2022/01/18 (YYYY/MM/DD)

Here's my code...

Sub MAIN_RTN()

Dim WK_GETDATE As Date
Dim WEBDRIVER As New Selenium.ChromeDriver
Dim IDX0 As Integer
Dim WK_DATE As String

    WEBDRIVER.AddArgument "disable-gpu"
    WEBDRIVER.AddArgument "start-maximized"
    With WEBDRIVER
        .Start
        .Get ThisWorkbook.Worksheets(1).Range("B5").Value
        .FindElementByName("txtID").SendKeys ThisWorkbook.Worksheets(1).Range("F2").Value
        .FindElementByName("txtPass").SendKeys ThisWorkbook.Worksheets(1).Range("F3").Value
    End With

    WK_DATE = ThisWorkbook.Worksheets(1).Range("F4").Value
    WK_DATE = Replace(WK_DATE, "/", "")
    WK_GETDATE = Mid(WK_DATE, 1, 4) & "/" & Mid(WK_DATE, 5, 2) & "/" & Mid(WK_DATE, 7, 2)
    
    For IDX0 = 11 To (ThisWorkbook.Worksheets(1).Range("B10")   10)
        WEBDRIVER.FindElementById("cphContents_ddlMc").SendKeys ThisWorkbook.Worksheets(1).Range("B" & IDX0).Value
        WEBDRIVER.FindElementById("cphContents_txtDayF").Clear
        Call WaitFor(1)
        Call DATE_SET(WEBDRIVER, WK_GETDATE, "cphContents_txtDayF")
        WEBDRIVER.FindElementById("cphContents_txtDayT").Clear
        Call WaitFor(1)
        Call DATE_SET(WEBDRIVER, WK_GETDATE   1, "cphContents_txtDayT")
        Call WaitFor(1)
        WEBDRIVER.FindElementById("cphContents_cmdExcel_Day").Click
        Call WaitFor(5)
    Next IDX0    
    WEBDRIVER.Quit
    Exit Function
End Sub

Sub DATE_SET(ByRef WEBDRIVER As Selenium.ChromeDriver, ARG_DATE As Date, WK_ELEMENT As String)

Dim IDX As Integer
Dim WKSTR1 As String
    WKSTR1 = Format(ARG_DATE, "YYYYMMDD")

    Call WaitFor(2)
    WEBDRIVER.FindElementById(WK_ELEMENT).Click

    Call WaitFor(2)
    For IDX = 1 To Len(WKSTR1)
        WEBDRIVER.FindElementById(WK_ELEMENT).SendKeys Mid(WKSTR1, IDX, 1)
    Next IDX
End Sub

CodePudding user response:

Have you tried using WEBDRIVER.ExecuteScript?

datescript = "arguments[0].setAttribute('value', 'yourdate')"
WK_ELEMENT = WEBDRIVER.FindElementById("cphContents_txtDayF")
WEBDRIVER.ExecuteScript datescript, WK_ELEMENT

CodePudding user response:

I found a workaround for this. I forced the cursor to move to the textbox leftmost side. I installed SeleniumWrapper for this to work. Press Enter and Down arrow key in VBA-Selenium

WEBDRIVER.FindElementById("cphContents_txtDayF").Click            
For x = 1 To 5                
WEBDRIVER.SendKeys keys.ArrowLeft            
Next x
  •  Tags:  
  • Related