Selenium WebDriver wait till element is displayed

后端 未结 9 777
死守一世寂寞
死守一世寂寞 2020-11-29 04:14

I have searched on Google and the SO site and I get answers for JAVA but do not seem to get answers for node.js

I have a web app that takes time to load. I would like

相关标签:
9条回答
  • 2020-11-29 04:32

    The main problem is webdriver thinks element is already there, but not yet. I have a solution, ugly but works. After the webdriver think item is there, try to click on. Get an error message:

    StaleElementReferenceError: stale element reference: element is not attached to the page document  (Session info: chrome=83.0.4103.106)
    

    No problem, in the loop waiting 500ms, and try to click on again. In my case 5 try is enough, about 2-3 click is success.

    async clickonitem( driver, itemname ) {
        const strftime = require('strftime');
        var trycounter = 0;
        var timeout = 500;
        var success;
        do {
          try {
            trycounter++;
            success = true;
            console.log( strftime('%F %T.%L'), "Finding #" + trycounter + " " + itemname );
            var item = await driver.wait( until.elementLocated( By.xpath( '//input[@name="' + itemname +'"]' ) ), 
                            timeout );
            console.log( strftime('%F %T.%L'), "Found or Timeout #" + trycounter );
            //await item.click();
            await driver.wait( item.click(), 
                            timeout );
            console.log( strftime('%F %T.%L'), "Click #" + trycounter + " " + itemname );
          } 
          catch(err) {
            success = false;
            //this.log( "Error #" + trycounter + " " + itemname + "\n" +err );
            this.log( strftime('%F %T.%L'), "Error #" + trycounter + " " + itemname + " waiting: " + timeout );
            await wait( timeout );
            continue;
          }
    
        } while( !success && trycounter < 5 );
    }       
            async wait( ms ) {
              return new Promise((resolve) => {
                setTimeout(resolve, ms);
              });
            }
    
    clickonitem( driver, "login_button" );
    
    0 讨论(0)
  • 2020-11-29 04:35

    You don't need a custom function, you can just do this:

    let el = await driver.findElement(By.id(`import-file-acqId:${acqId}`));
    await driver.wait(until.elementIsVisible(el),100);
    await el.sendKeys(file);
    

    See the docs here.

    0 讨论(0)
  • 2020-11-29 04:39

    Try something like this:

    function isItThere(driver, element){
    
        driver.findElement(webdriver.By.id(element)).then(function(webElement) {
                console.log(element + ' exists');
            }, function(err) {
                if (err.state && err.state === 'no such element') {
                    console.log(element + ' not found');
                } else {
                    webdriver.promise.rejected(err);
                }
            });
    }
    

    I adapted it slightly based on what I found here: Check if element exists - selenium / javascript / node-js and it worked a charm.

    0 讨论(0)
提交回复
热议问题