這里很奇怪。手動測驗時,我單擊洗掉按鈕,等待彈出視窗
然后單擊確定并按預期洗掉記錄。但是當我嘗試在 Java/Selenium 中做同樣的事情時,它是這樣的->
WebElement element = _driver.findElement(By.id("btnDeletePatient"));
JavascriptExecutor executor = (JavascriptExecutor)_driver;
executor.executeScript("arguments[0].click();", element);
或者
_driver.findElement(By.id("btnDeletePatient")).click();
兩者都有相同的回應,確定/取消彈出視窗將出現,然后立即消失。
這是按鈕的代碼
<input type="submit" name="ctl00$cpContentLeft$btnPatient"
value="Delete User" onclick="return userDelete();"
id="ctl00_cpContentLeft_btnPatient"
tabindex="81" class="btn btn-outline-primary btn-sm mr-3">
這是函式 userDelete 的代碼
function userDelete() {
if (confirm("Are you sure you wish to delete this user?")) {
return true;
}
else {
return false;
}
}
此外,我現在在 Edge 中嘗試了相同的方法,同樣的事情,所以這似乎不是 Chrome 的問題。
任何人都知道是什么導致了這種情況?
進一步測驗如下。我在腳本將單擊洗掉運行并運行腳本的位置放置了一個斷點。頁面按預期加載,我到達了問題點。
我手動單擊洗掉按鈕,彈出視窗出現,然后一直停留,直到我單擊取消。
使用此代碼我逐步執行,彈出視窗出現然后立即消失。
_driver.findElement(By.id("ctl00_cpContentLeft_btnDelete")).click();
使用此代碼,回應為 2。
_driver.findElement(By.id("ctl00_cpContentLeft_btnDeletePatient")).sendKeys(Keys.SPACE);
然后我嘗試雙擊,但沒有任何反應。在所有測驗中,我都沒有在控制臺中看到任何錯誤。
uj5u.com熱心網友回復:
我希望您在頁面完全加載之前單擊洗掉按鈕,這就是 pop 突然消失的原因。請在單擊洗掉按鈕之前添加等待。
腳步:
- 完全加載頁面(在此處添加等待)
- 點擊洗掉按鈕
- 單擊確定按鈕。
請在此處添加更多代碼和螢屏截圖。那會更有幫助
uj5u.com熱心網友回復:
這看起來像一個 java 腳本警報。在 selenium 中,我們有特定的方式來處理警報。您不需要單擊確定。您可以先等待警報出現。如果存在,則切換到該警報。
然后,我們有特定的方法來處理 selenium 中的警報。
alert.accept() 點擊確定。
alert.dismiss() 點擊取消。
硒檔案中的示例代碼:
driver.findElement(By.linkText("See a sample prompt")).click();
//Wait for the alert to be displayed and store it in a variable
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
//Type your message
alert.sendKeys("Selenium");
//Press the OK button
alert.accept();
//Press the Cancel button
alert.dismiss();
//Store the alert in a variable for reuse
String text = alert.getText();
uj5u.com熱心網友回復:
好的找到了
問題出在 chromedriver 中,這條線修復了它
chromeOptions.setCapability("unexpectedAlertBehaviour", "ignore");
現在彈出視窗會一直保留,直到在腳本中執行操作。
我們生活和學習,感謝您的幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/338312.html
標籤:javascript 爪哇 硒
上一篇:如何在selenium、python中列印文本(按類名)?
下一篇:無法插入串列
