Java Selenium-从网页停止脚本
有时,当我想加载一个页面时,脚本会出现问题并阻塞浏览器 使用此html,我可以模拟确切的问题:Java Selenium-从网页停止脚本,java,selenium-webdriver,Java,Selenium Webdriver,有时,当我想加载一个页面时,脚本会出现问题并阻塞浏览器 使用此html,我可以模拟确切的问题: <html> <head> <script> while(true){} </script> </head> <body> <div id='stuck'> I'm stuck. &l
<html>
<head>
<script>
while(true){}
</script>
</head>
<body>
<div id='stuck'>
I'm stuck.
</div>
</body>
</html>
while(true){}
我卡住了。
我尝试在上面的页面上运行我的代码:
ChromeOptions coptions = new ChromeOptions();
coptions.setCapability("pageLoadStrategy", "none");
//rootOfProject/data/chromedriver.exe
System.setProperty("webdriver.chrome.driver", "data/chromedriver.exe");
WebDriver driver = new ChromeDriver(coptions);
driver.manage().window().maximize();
driver.get("file:///.../stuckbrowser.html");
WebDriverWait wdw = new WebDriverWait(driver, 10);
//using debugger it stuck here
//more than 10 seconds(infintely) - because browser is frozen from script in <head>
String text = wdw.until(ExpectedConditions.visibilityOfElementLocated(By.id("stuck"))).getText();
System.out.println(text);
driver.quit();
System.out.println("Done");
ChromeOptions copions=新的ChromeOptions();
设定能力(“页面加载策略”、“无”);
//rootOfProject/data/chromedriver.exe
System.setProperty(“webdriver.chrome.driver”、“data/chromedriver.exe”);
WebDriver=新的ChromeDriver(coptions);
driver.manage().window().maximize();
驱动程序。获取(“file:///.../stuckbrowser.html");
WebDriverWait wdw=新的WebDriverWait(驱动程序,10);
//使用调试器,它卡在这里了
//超过10秒(无限)-因为浏览器已从中的脚本冻结
String text=wdw.until(ExpectedConditions.visibilityOfElementLocated(By.id(“卡住”)).getText();
System.out.println(文本);
driver.quit();
系统输出打印项次(“完成”);
当我尝试使用id=stack
获取元素的getText()
时,我的代码被卡住了
它停留在那里超过10秒(多少?我不知道。我想它是无限的)
有一种方法可以停止运行时间超过x秒的脚本(从网页中)?您可以发布初始化web驱动程序的代码吗?你可能想读书。问题是关于Selenium和Python的,但Java中的答案可能类似。@GregBurghardt我用如何启动驱动程序的信息更新了我的问题。你能发布初始化web驱动程序的代码吗?你可能想读书。问题是关于Selenium和Python的,但Java中的答案可能类似。@GregBurghardt我用如何启动驱动程序的信息更新了我的问题。