首页 > 解决方案 > 有时 InvalidArgumentError: 'handle' 必须是字符串

问题描述

我安装了“React-google-login”来响应项目。我正在编写一个应该绕过这个模块的自动测试。

try {
  await driver.get("http://localhost:3000/");
  await driver.wait(until.elementLocated(By.xpath(`//*[@id="root"]/div/button`)), 10000).click();
  await driver.getAllWindowHandles().then(async function(handles){
      await driver.switchTo().window(handles[1])
        .then(async function(){    // ERROR: InvalidArgumentError: invalid argument: 'handle' must be a string
          await driver.wait(until.elementLocated(By.id('identifierId')),20000).sendKeys('test@gmail.com', Key.ENTER)
          await driver.wait(until.elementLocated(By.name('password')),20000).sendKeys('test1234', Key.ENTER)  // ERROR: ElementNotInteractableError: element not interactable
      });
  });
  } catch (e) {
  console.log(e)
  }

在某些情况下,代码可以满足,在某些情况下,错误是:

InvalidArgumentError: invalid argument: 'handle' must be a string

在某些情况下,错误是:

ElementNotInteractableError: element not interactable    

请告诉我,我的代码有什么问题?

标签: javascriptreactjsseleniumselenium-chromedriver

解决方案


这个解决方案决定了我的问题。

      try {
         await driver.get("http://localhost:3000/");
         await driver.wait(until.elementLocated(By.xpath(`//*[@id="root"]/div/button/div`)), 10000)  // wait this element
         await driver.findElement(By.xpath(`//*[@id="root"]/div/button`)).click();                   // -> after click on it
         let winHandleBefore = driver.getWindowHandle();                                             // first window value 
       await driver.getAllWindowHandles().then(async  function(handles){
         await driver.switchTo().window(handles[1]).then(async function(){   
            await driver.wait(until.elementLocated(By.id('identifierId')),20000);                    // wait this element
       await driver.findElement(By.id('identifierId')).sendKeys('test@gmail.com',      Key.ENTER)    // after input value
             await driver.wait(until.elementLocated(By.name('password')),20000);                     // wait this element
             await driver.findElement(By.name('password')).sendKeys('test1234', Key.ENTER)           // after input value
         });
       });
       driver.switchTo().window(winHandleBefore);                                                    // Change to the first window
       await driver.wait(until.elementLocated(By.xpath(`//*[@id="root"]/div/img`)), 10000)
       /* Any actions */
       } catch (e) {
         console.log(e)        
       } 

推荐阅读