我必須使用 Excel VBA 中的 selenium basic 將 Web 自動化程式從 Excel VBA 控制的 IE 遷移到 Chrome。該網站本身不公開,因此我無法分享鏈接。我遇到問題的方面是一個顯示 2 周日歷的頁面,默認從今天開始,以便輸入特定后續日期的事件。我想將默認日期更改為另一個日期,并讓網站更改顯示的日期以從該新日期開始。這涉及更改 ID = "_ctl1_currentDate" 的隱藏輸入 web 元素的 "Value" 屬性,該元素使用 VBA excel 中的本機驅動程式與 IE 一起使用,但我無法讓它與用于 chrome 的 Excel VBA 的 Selenium Basic 驅動程式一起使用。
這是 web 元素的 html:
<input name="_ctl1:currentDate" type="hidden" id="_ctl1_currentDate" value="19/01/2022">
這是與此 web 元素關聯的 Javascript:
<script language="javascript">
/* une fonction permettant de se déplacer dans calendrier */
function move(direction) {
var date = document.getElementById("_ctl1_currentDate").value;
var day;
var month;
var year;
var t = new Date();
var newDate;
var newMonth;
var newDay;
if (date.charAt(2) == "/") {
day = date.substring(0, 2);
if (date.charAt(5) == "/") {
month = date.substring(3, 5);
year = date.substring(6, 10);
} else {
month = date.substring(3, 4);
year = date.substring(5, 9);
}
} else {
day = date.substring(0, 1);
if (date.charAt(4) == "/") {
month = date.substring(2, 4);
year = date.substring(5, 9);
} else {
month = date.substring(2, 3);
year = date.substring(4, 8);
}
}
t.setDate(1);
t.setYear(year);
t.setMonth(month - 1);
t.setDate(day);
t = DateAdd("d", t, direction * 10);
newMonth = t.getMonth() 1;
newDay = t.getDate();
if (newDay < 10) { newDay = "0" newDay; }
if (newMonth < 10) { newMonth = "0" newMonth; }
newDate = newDay "/" newMonth "/" t.getFullYear();
document.getElementById("_ctl1_currentDate").value = newDate;
document.getElementById("aspnetForm").submit();
}
/* cette fonction fait un submit de la form globale TheForm, présente sur chaque page de portail */
function redirectToQstWithDate(typeQST, OptMA, date) {
var theForm = getTheForm();
theForm.action = '' document.getElementById("_ctl1_redirectUrl").value '';
if (OptMA && OptMA != "") {
document.getElementById('OPTMA').value = OptMA;
}
document.getElementById('QST').value = typeQST;
document.getElementById('ScreenSize').value = window.screen.width;
document.getElementById('InputDate').value = date;
__doPostBack('', '');
}
</script>
這是以前適用于 IE 的代碼,其中“jumpdate”(字串)=我要替換默認(當前)日期的新日期:
IE.document.getElementById("_ctl1_currentDate").Value = jumpDate
IE.document.getElementById("aspnetForm").submit
這是與 selenium basic 中上述代碼的第一行等效的新嘗試,它產生 SeleniumError "Element not interactable"。"ch" 是 Chrome 驅動程式參考:
ch.FindElementById("_ctl1_currentDate").SendKeys (jumpDate)
我覺得 Selenium 不允許我在 chrome 中更改此元素的值,盡管可以在沒有 Selenium 的情況下為 IE 做到這一點。我認為相反,我需要觸發相關的 Javascript;這本質上是我對舊 IE 代碼的第二行所做的,同時改變了與舊 IE 代碼的第一行的爭論。不幸的是,我無法在 Selenium Basic 中使用 .executeScript 函式獲得正確的語法來執行此操作。任何有關使用硒驅動程式的正確 VBA 代碼的建議都將不勝感激!
uj5u.com熱心網友回復:
selenium 基本使用ExecuteScript方法設定 value 屬性的翻譯如下,您在 jumpDate 值中連接
.ExecuteScript "document.getElementById('_ctl1_currentDate').setAttribute('value','" & jumpDate & "');"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/417984.html
標籤:
