2017-08-11 52 views
0

我在SOF here上閱讀了一篇文章,並試圖在自己的問題解決中遵循它,但是我讓自己變得更加困難而不是解決方案。selenium webdriver promise-all confusion

我試圖findElements,從網站定位的

<img id="4444" class="someclass" src="/images/animg.png" onclick="doSomething(this)" title="contact person Name"/> 

一個數組,我想在一個循環中的每個進一步點擊並執行每每個操作。

由於所有img標籤具有屬性 「SRC = /圖片/ animg.png」,我寫道:

drv.wait(until.elementLocated(By.xpath(".//*[@src='/images/animg.png']")), 10000, 'img not appeared'); 
drv.findElements(By.xpath(".//*[@src='/images/animg.png']")).then(function (imgs) { 
    let myimgs = imgs.map(function (elem) { 
     elem.getAttribute("src").then(function (x) { 
      return elem; //or try elem.getAttribute("src"); 
     }); 
    }); 
    thepromise.all(myimgs).then(function (allit) { 
     console.log('y: ' + allit[0]); 
    }); 

}); 

的困惑:
1.如果我發出了 「迴歸ELEM」,進一步做「 elem.click()「,我得到一個承諾對象(使用util.inspect容易看到)
2.我做出結論,這elem,是一個承諾對象
3.如果我發出」return elem.getAttribute 「SRC」);」我有src返回,這讓我認爲elem不是一個承諾,但
4.不幸的是,試圖發佈「elem.click()」的allit [0] .click()「失敗,因爲它顯示一個承諾對象!

所以我很困惑那裏真的發生了什麼。你能解釋一下嗎? (我搜索了半天,仍然無法理解任何地方)。

回答

1

的問題是,你忘了在線路返回地圖的價值:

let myimgs = imgs.map(function (elem) { 
    elem.getAttribute("src").then(function (x) { 
    ... 

見下面我的例子。

driver.get("http://buaban.com"); 
driver.wait(until.elementLocated(By.xpath(".//*[contains(@src,'.png')]")), 10000, 'img not appeared'); 

driver.findElements(By.xpath(".//*[contains(@src,'.png')]")).then(function (imgs) { 
    // Example 1 - return string 
    let allImgSrcs = imgs.map(function (elem) { 
     return elem.getAttribute("src").then((src)=>{ 
      return "test 1 - " + src; 
     }); 
    }); 

    Promise.all(allImgSrcs).then(function (imgSrcs) { 
     imgSrcs.forEach((imgSrc)=>{ 
      console.log(imgSrc); 
     }); 
    }); 

    // Example 2 - return string 
    let allImgSrcs2 = imgs.map(function (elem) { 
     return elem.getAttribute("src").then((src)=>{ 
      return elem.getAttribute("src").then(()=>{ 
       return "test 2 - " + src; 
      }); 
     }); 
    }); 

    Promise.all(allImgSrcs2).then(function (imgSrcs) { 
     imgSrcs.forEach((imgSrc)=>{ 
      console.log(imgSrc); 
     }); 
    }); 

    // Example 3 - filter and then return WebElement 
    let allSeleniumElements = imgs.map(function (elem) { 
     return elem.getAttribute("src").then((src)=>{ 
      if(src.indexOf("selenium")>0) { 
       return elem; 
      } 
     }); 
    }); 

    Promise.all(allSeleniumElements).then(function (imgEls) { 
     imgEls.forEach((ele)=>{ 
      if(typeof ele !=="undefined") { 
       //console.log(ele); 
       ele.getAttribute("src").then((src)=>{ 
        console.log("Test 3 - " + src); 
       }); 
      } 
     }); 
    }); 

    // Example 4 - Click WebElement 
    imgs.forEach((ele)=>{ 
     ele.click(); 
    }); 

}); 

控制檯將是:

test 1 - http://www.buaban.com/wp-content/uploads/2017/08/Logo-sublime-3.png 
test 1 - http://www.buaban.com/wp-content/uploads/2016/07/selenium-logo-featured.png 
test 1 - http://www.buaban.com/wp-content/uploads/2016/07/selenium-logo-featured.png 
test 1 - http://www.buaban.com/wp-content/uploads/2017/04/msdn_new_logo-edit.png 
test 1 - http://www.buaban.com/wp-content/uploads/2016/07/apachejmeter-crop.png 
test 1 - http://www.buaban.com/wp-content/uploads/2016/07/apachejmeter-crop.png 
test 2 - http://www.buaban.com/wp-content/uploads/2017/08/Logo-sublime-3.png 
test 2 - http://www.buaban.com/wp-content/uploads/2016/07/selenium-logo-featured.png 
test 2 - http://www.buaban.com/wp-content/uploads/2016/07/selenium-logo-featured.png 
test 2 - http://www.buaban.com/wp-content/uploads/2017/04/msdn_new_logo-edit.png 
test 2 - http://www.buaban.com/wp-content/uploads/2016/07/apachejmeter-crop.png 
test 2 - http://www.buaban.com/wp-content/uploads/2016/07/apachejmeter-crop.png 
Test 3 - http://www.buaban.com/wp-content/uploads/2016/07/selenium-logo-featured.png 
Test 3 - http://www.buaban.com/wp-content/uploads/2016/07/selenium-logo-featured.png 
+0

這是目前有關返回屬性明確。非常感謝,我很抱歉,我無法清楚地解釋我的意圖。實際上,我的最終目標是將img作爲元素返回,然後在循環中單擊它們,也許是發生器產量。你能不能告訴我,我怎麼可以做一些像「return elem.then()」的東西來最後發佈「elem.click()」?正如我上面解釋的那樣,這個img有一個屬性「onclick = doSomething(this)」,我最終的理由是強制它運行,並且它是2天我不能擁有它! –

+0

@AdrianDain我已經添加了示例#4。那是你要的嗎? – Buaban

+0

也許對於elem.click()我甚至不需要使用promise,並在findElements正確後立即執行它? –