【问题标题】:An alert can be accepted when loading page with selenium on internet explorer?在 Internet Explorer 上加载带有 selenium 的页面时可以接受警报吗?
【发布时间】:2021-09-29 20:38:47
【问题描述】:

我正在尝试使用 selenium 和 node.js 获取 url,但是当站点有警报时,我无法在 Internet Explorer 上接受它。

index.js

    require('iedriver');
    const express = require('express');
    const webdriver = require('selenium-webdriver');
    let ie = require('selenium-webdriver/ie');
    function start(params) {
        start_server();
        let options = new ie.Options();     
          options.ignoreZoomSetting(true);
    
        let driver = new webdriver.Builder()
        .forBrowser('internet explorer')
        .withCapabilities(options)
        .build();
    
        let site="http://127.0.0.1:3000/";
        driver.get(site)
        .then(()=>{
            return driver.wait(webdriver.until.alertIsPresent(),10000)
            .then(()=>{
                let alert = driver.switchTo().alert();
                return alert.accept()
                console.log("go on");
            })
        })
        ;
    }
    const start_server=()=>{
    
        const app = express();
        const PORT = 3000;
      
        app.use(express.static('static'));
            
        app.get('/', function(req, res){
          let options = {
            root: path.join(__dirname+"/../static/")
          };
          
          let fileName = 'index.html';
          res.sendFile(fileName, options, function (err) {
            if (err) {
              log(err);
            } else {
              console.log('Sent:', fileName);
            }
          });
        });
        
        app.listen(PORT, function(err){
          if (err) console.log(err);
          console.log("Server listening on PORT", PORT);
        });
          
      };
      start();

要打开的网站在页面加载时会有提示,如下所示:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="loading.js"></script>
    <title>Document</title>
</head>
<body>
       
</body>
</html>

加载.js

alert("accept before start");

这必须在 Internet Explorer 上运行。

运行时,5分钟后显示这个

UnhandledPromiseRejectionWarning: TimeoutError: Timed out waiting for page to load.
at Object.throwDecodedError (C:\d\adhoc\node\copyimgwz\node_modules\selenium-webdriver\lib\error.js:517:15)
    at parseHttpResponse (C:\d\adhoc\node\copyimgwz\node_modules\selenium-webdriver\lib\http.js:642:13)

就这样停下来

blocking alert on ie with selenium

请问您知道如何使用 selenium 接受该警报吗?

编辑

我找到了一个解决方案,添加这行它已经可以正常工作了

options.introduceFlakinessByIgnoringProtectedModeSettings(true);

【问题讨论】:

  • 嗯,看来你的问题已经解决了。您可以将解决方案作为答案发布,并在可以标记时进行标记。它可以在未来帮助其他社区成员解决类似的问题。感谢您的理解。

标签: node.js selenium-webdriver internet-explorer alert pageload


【解决方案1】:

我能够让它工作,添加这一行

options.introduceFlakinessByIgnoringProtectedModeSettings(true);

在这个部分

let options = new ie.Options();     
options.ignoreZoomSetting(true);
options.introduceFlakinessByIgnoringProtectedModeSettings(true);

显然,在 Internet Explorer 上,您需要激活此功能才能让您工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    相关资源
    最近更新 更多