2017-08-11 9 views
0

私はSOF hereの投稿を読んで、私自身の問題解決でそれに従おうとしましたが、解決策よりも自分自身を問題にしました。セレンのwebdriver約束 - すべての混乱

私はウェブサイトから

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

の配列をターゲットに、findElementsに試みたが、私はさらに、ループ内の各をクリックすると、各あたりの操作を実行したいと思います。すべての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" を発行した場合」
2.私は、この要素が約束オブジェクトであるという結論を下しました
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を返し、おそらく発電機の利回りをループでクリックすることです。あなたは親切に私に "elem.click()"を発行するために、 "return elem.then()"のようなことをどうやってできるか教えてください。私が上で説明したように、このimgは "onclick = doSomething(this)"という属性を持っています。私の最終的な理由はそれを強制的に実行して2日です。 –

+0

@AdrianDain例#4を追加しました。それはあなたが欲しいものですか? – Buaban

+0

多分elem.click()のために私は約束を使用する必要はなく、findElementsが正しい直後にそれを行うことさえできますか? –

関連する問題