1
votes

I have searched for a while but cannot find a solution. also find similar link on this page and it is locked due to duplcation but actually it is not duplicate of anything. this is not Java or #c or anyother mentioned languages. this is vba and selenium codes. on below link on debugging mode (F8) everything working smoothly however on run mode (F5) it is not filling datefrom and dateto section or filling partily.

do you have any idea? also web site is a corporate web site so unfortunately I cannot share it.

Option Explicit
Sub Download_Report4()
Dim obj As New Selenium.WebDriver
Dim ele As WebElement
obj.Start "Chrome"
obj.Get "https://test.com"
Dim Username, Password As Range
Set Username = ActiveWorkbook.Sheets("LoginData").Range("B1")
Set Password = ActiveWorkbook.Sheets("LoginData").Range("B2")
           obj.FindElementById("UserName").SendKeys Username.Text
            obj.FindElementById("Password").SendKeys Password.Text
            obj.FindElementById(LoginButton").Click
            Application.Wait Now + TimeValue("00:00:02")

     Dim Rows, x

For x = 2 To Sheet1.UsedRange.Rows.count Step 1

    If IsEmpty(Sheet1.Range("A" & x)) Then
        Exit For
    Else

obj.FindElementByName("date$From").Clear
obj.FindElementByName("date$To").Clear


    obj.FindElementByName("date$From").SendKeys ("01,03,2017")
    obj.FindElementByName("date$To").Click
       obj.FindElementByName("date$To").SendKeys (Sheet1.Range("B" & x))
       obj.FindElementById("btnSearch").Click

       obj.FindElementById("btndownload").Click
End If
Next


End Sub

by the way, date format is correct. when it is entered as 01/03/2017 chrome add dates odd. month section to date year is correct and nothing add to month section.

I will giving little bit more details. on below code for internet explorer is working very well unless after download button click, ie 11 prompt "do you want to open or save" question stops my macro and loops ending. this is the reason why I move the macro selenium.

Public Sub Download_Report()
    Dim ie As New InternetExplorer
    Dim myElem As Variant
        ie.Visible = True
        ie.navigate "https://test.com"

            ie.document.getElementById("UserName").Focus
            ie.document.getElementById("LUserName").Value = Sheets("LoginData").Range("B1").Text
            ie.document.getElementById("Password").Focus
            ie.document.getElementById("Password").Value = Sheets("LoginData").Range("B2").Text
            ie.document.getElementById("LoginButton").Click
            End If

     Dim Rows, x
 For x = 2 To Sheet1.UsedRange.Rows.count Step 1

    If IsEmpty(Sheet1.Range("A" & x)) Then
        Exit For
    Else
                    While .Busy Or .readyState < 4: DoEvents: Wend
        ie.document.getElementsByName("date$From")(0).Value = Sheet1.Range("A" & x).Text
                    While .Busy Or .readyState < 4: DoEvents: Wend
        ie.document.getElementsByName("date$To")(0).Value = Sheet1.Range("B" & x).Text
                    While .Busy Or .readyState < 4: DoEvents: Wend
        ie.document.getElementById("btnSearch").Click
                    While .Busy Or .readyState < 4: DoEvents: Wend
        ie.document.getElementById("btndownload").Click
End If
Next
End With
End Sub

date can be use as picker after clicked but also can be filled as text also, date section code on web site as is below. also date to section is as same as . Website code (website is behind a firewall)

<input name="date$From" type="text" value="18/02/2020" size="6" id="date_tbFrom" class="searchSelect">
<input type="hidden" name="date_From_Client" id="date_From_Client">
1
If something works only when debugging that's often an indication of a timing problem - you're trying to access something on the page which hasn't yet been rendered. Also I'm not familiar with Selenium but you can't use Value instead of SendKeys?Tim Williams
@TimWilliams I have just edited question. also for short version. on web site when click the date section date picker is pop up. at the same time you can enter date as text to date section. also, ı have adde obj.wait(2000) after each section to being sure about time. after complete 2 days ı am still on the same pointSelpaqM
You will need multiple wait time or try using WebDriverWait for page to load complete0m3r
it is already tried with obj.wait(500) Dim obj As New Selenium.WebDriverSelpaqM

1 Answers

0
votes

yeah there were issue that (I do not know the why but), when .click command is applied to date section cursoir was choosen the half of the section or the end so date was entered by macro is wrong or section cannot be filled that means empty. however at debugging mod everything was working smoothly. than I find another solution. I have find sections one tab before date sections. then added

obj.FindElementByName("date$From").Clear
obj.FindElementByName("date$To").Clear
obj.findelementsbyid("sectionbeforedatefrom").click
obj.sendkeys(keys.Tab)
obj.FindElementByName("date$From").SendKeys ("01,03,2017")
obj.findelementsbyid("sectionbeforedateto").click
obj.sendkeys(keys.Tab)
obj.FindElementByName("date$To").SendKeys (Sheet1.Range("B" & x))
obj.FindElementById("btnSearch").Click

those steps worked for me. but @TimWilliams was right all things are related with time. thanks everyone.