我正在编写一个 Windows 应用程序,它可以浏览 WebBrowser 控件并根据某些条件自动填写表单。在一些特定的表单上,会提取一些日期,以便根据该条件检索某些信息。问题是,在星期一,开始日期必须是前一周的星期五。对于所有其他日期(周二至周五),开始日期为前一天。我正在尝试使用 If 语句来解决此问题,但是当我运行表单时,它没有填写正确的日期。
这是我正在使用的:
Public Class Form1
Inherits Form
Dim otherdays = Format(Now.AddDays(-1), "M/d/yyyy")
Dim mondays = Format(Now.AddDays(-3), "M/d/yyyy")
Dim current = Format(Now, "M/d/yyyy")
Private Sub approval1()
WebBrowser1.Document.GetElementById("txtDate").SetAttribute("Value", mondays)
WebBrowser1.Document.GetElementById("Search").InvokeMember("click")
End Sub
Private Sub approval2()
WebBrowser1.Document.GetElementById("txtDate").SetAttribute("Value", otherdays)
WebBrowser1.Document.GetElementById("Search").InvokeMember("click")
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If ComboBox1.SelectedItem = "IM Approval" Then
WebBrowser1.Navigate("https:")
WaitForPageLoad()
'This is the part that does not work:
If Now.Day = Day.Monday Then
approval1()
WaitForPageLoad()
WebBrowser1.Navigate("javascript: ('','')")
Else
approval2()
WaitForPageLoad()
WebBrowser1.Navigate("javascript: ('','')")
End If
End If
End Sub
通过 Javascript 进行浏览的 Web 浏览器不是问题。填写日期是唯一的问题。
最佳答案
您对星期几的检查不起作用的原因是您正在检查月份的哪一天。这是 for 类的更简单版本,其中 if 语句已更正。
Public Class Form1
Inherits Form
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If ComboBox1.SelectedItem = "IM Approval" Then
WebBrowser1.Navigate("https:")
WaitForPageLoad()
Dim adjust As Integer = If(Now.DayOfWeek = DayOfWeek.Monday, -3, -1)
WebBrowser1.Document.GetElementById("txtDate").SetAttribute("Value", Format(Now.AddDays(adjust), "M/d/yyyy"))
WebBrowser1.Document.GetElementById("Search").InvokeMember("click")
WaitForPageLoad()
WebBrowser1.Navigate("javascript: ('','')")
End If
End Sub
End Class
关于javascript - 在 IF 语句中使用 VB 中的 'Day' 函数来更改日期并根据工作日发送表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28434461/