playwright f 對日期選擇控制元件的設定,直接使用js指令碼
public partial class MainForm : Form
{
IPlaywright playwright;
IPage page;
public MainForm()
{
InitializeComponent();
}
private async void MainForm_Load(object sender, EventArgs e)
{
dtpStart.Value = DateTime.Now.Date.AddDays(-366);
cbxDkyy.SelectedIndex = 1;
playwright = await Playwright.CreateAsync();
var userDataDir = $"{Directory.GetCurrentDirectory()}\\UserData";
var context = await playwright.Chromium.LaunchPersistentContextAsync(userDataDir,
new BrowserTypeLaunchPersistentContextOptions
{
Headless = false,
ViewportSize = new ViewportSize()
{
Width = 1920,
Height = 1080
}
}
);
page = context.Pages[0];
await page.GotoAsync("https://www.dacong.com/");
await page.ClickAsync("input[type=\"text\"]");
// Fill input[type="password"]
await page.FillAsync("input[type=\"password\"]", "a1234567dc");
// Press Enter
await page.RunAndWaitForNavigationAsync(async () =>
{
await page.PressAsync("input[type=\"password\"]", "Enter");
});
}
private async Task SendDataAsync()
{
var strAry = txtCode.Text.Split(',');
if (strAry.Length < 4)
{
MessageBox.Show("不是有效的條碼!");
return;
}
try
{
var fphm = strAry[2];
await page.FillAsync("#fphm", fphm);
// Click #search >> text=查詢
await page.ClickAsync("#search >> text=查詢");
await page.WaitForTimeoutAsync(300);
// Check input[name="checkbox1"]
await page.CheckAsync("input[name=\"checkbox1\"]");
if (rbtnBdk.Checked)
{
var code = cbxDkyy.Text.Substring(0, 1);
await page.SelectOptionAsync("#b3bdklx", new[] { code });
}
btnSend.Enabled = true;
// Click text=提交
await page.ClickAsync("text=提交", new() { Timeout = 40500 });
// Click input:has-text("確定")
await page.ClickAsync("input:has-text(\"確定\")");
// Click input:has-text("確定")
await page.ClickAsync("input:has-text(\"確定\")");
txtCode.Clear();
}
catch (Exception)
{
btnSend.Enabled = true;
}
}
private async void btnSend_Click(object sender, EventArgs e)
{
btnSend.Enabled = false;
await SendDataAsync();
}
private async void txtCode_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
await SendDataAsync();
}
private async void btnSetrq_Click(object sender, EventArgs e)
{
//日期 開始
//await page.ClickAsync("#sjq");
//await page.FillAsync("#sjq", dtpStart.Value.ToString("yyyy-MM-dd"));
// //await page.PressAsync("#sjq", "Enter");
// // Fill text=日期: - >> input[type="text"]
// await page.PressAsync("#sjq", "Enter");
// await page.ClickAsync("#sjq");
// await page.WaitForTimeoutAsync(3000);
// // 日期-截止
// await page.ClickAsync("#sjz");
// await page.PressAsync("#sjz", "Shift+End");
// await page.FillAsync("#sjz", dtpEnd.Value.ToString("yyyy-MM-dd"));
// await page.PressAsync("#sjz", "Enter");
// await page.WaitForTimeoutAsync(3000);
//直接使用js指令碼playwright,My97 DatePicker
//await page.EvaluateAsync($"$('#sjq\').val('{dtpStart.Value.ToString("yyyy-MM-dd")}');$('#sjz').val('{dtpEnd.Value.ToString("yyyy-MM-dd")}')");
await page.EvaluateAsync($"$('#sjq\').val('{dtpStart.Value.ToString("yyyy-MM-dd")}');");
txtCode.Focus();
}
private void btnReset_Click(object sender, EventArgs e)
{
txtCode.Clear();
txtCode.Focus();
}
}