HtmlUnitDriver를 사용하여 경고 이벤트를 처리하려고하지만 몇 가지 문제가 있으며 그 이유를 알고 싶습니다. 여기 자바 코드 :java에서 HtmlUnitDriver로 경고를 처리하는 방법은 무엇입니까?
HtmlUnitDriver browser = new HtmlUnitDriver(true);
browser.get("http://localhost:8001/index.html");
browser.findElementById("myButton").click();
try {
WebDriverWait wait = new WebDriverWait(browser, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = browser.switchTo().alert();
alert.accept();
System.out.println("ALERT");
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("NO ALERT");
}
String htmlContent = browser.getPageSource();
System.out.println(htmlContent);
browser.close();
이 HTML 코드이다 index.html을
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta charset="utf-8" />
</head>
<body>
<form id="form1">
<input id="username" name="username" />
<button id="myButton" type="button" value="Page2">Go to Page2</button>
</form>
</body>
</html>
<script>
document.getElementById("myButton").onclick = function() {
location.href = "page2.html";
};
</script>
page2.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Page2</title>
<meta charset="utf-8" />
</head>
<body onload="myfunction('hello2')">
<p id="result"></p>
</body>
</html>
<script>
function myfunction(data) {
document.getElementById('result').innerHTML = data
}
</script>
콘솔의 출력은 :
NO ALERT
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>
Page2
</title>
<meta charset="utf-8"/>
</head>
<body onload="myfunction('hello2')">
?
<p id="result">
hello2
</p>
<script>
//<![CDATA[
function myfunction(data) {
document.getElementById('result').innerHTML = data
}
//]]>
</script>
</body>
</html>
출력을 보면, 소스 코드와 약간 다른 것 같아요. 이것에 대해서는 몇 가지 질문이 있습니다. page2.html에 대한 경고가 감지되지 않는 이유는 무엇입니까? 왜 "?"와 같은 몇 가지 추가 문자가 있습니까? 및 "// <! [CDATA ["? 어떻게 피할 수 있습니까?
나는 경보를 처리하려고하는데, 나는 처음부터 모든 제안을 감사하게 생각합니다.
정말 죄송 합니다만, onload에서 기능을 변경하지 않습니다. alert()을 작성하면 올바르게 작동합니다. 많은 감사합니다. – d3llafr33