2015-09-25 63 views
1

按鈕,我一定要考這個FB按鈕:點擊Facebook的 '分享' 與硒的Java

enter image description here

的Facebook按鈕的iframe內。我切換到iframe,我可以找到'分享'按鈕。但是當webDriver點擊它時,彈出窗口不顯示。我的代碼是:

WebElement iframeFacebook = webDriver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[3]/iframe[1]")); 
webDriver.switchTo().frame(iframeFacebook); 
webDriver.findElement(By.xpath("//span[contains(@class, 'pluginButtonLabel') and contains(., 'Share')]")).click(); 
webDriver.switchTo().defaultContent(); 

我對Twitter按鈕做同樣的工作。 FB按鈕有什麼特別之處?

編輯

FB按鈕的HTML代碼:

<iframe frameborder="0" allowtransparency="true" style="border:none; overflow:hidden; width:97px; height:35px; float:left" scrolling="no" src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Ffacebook.com%2Fmynumber26&width&layout=button&action=like&show_faces=false&share=true&height=35&appId=1454011268146569&locale=en_US"> 
    <!DOCTYPE html> 
    <html id="facebook" class="" lang="en"> 
    <body class="plugin gecko win x1 Locale_en_US" dir="ltr"> 
     <div class="_li"> 
      <div class="pluginSkinLight pluginFontHelvetica"> 
       <div> 
        <table class="uiGrid _51mz pluginConnectButtonLayoutRoot _3c9t" cellspacing="0" cellpadding="0"> 
         <tbody> 
          <tr class="_51mx"> 
           <td class="_51m- hCent"> 
            <td class="_51m- pluginButtonSpacer"> 
             <div class="pluginButton pluginButtonSmall" onclick="require(" Popup ").open("\/sharer.php?u=http\u00253A\u00252F\u00252Ffacebook.com\u00252Fmynumber26&display=popup&ref=plugin&src=share_button&app_id=1454011268146569 ", 670, 340);"> 
              <div> 
               <div class="pluginButtonContainer"> 
                <div class="pluginButtonImage"> 
                 <span class="pluginButtonLabel">Share</span> 
                </div> 
               </div> 
              </div> 
             </div> 
            </td> 
            <td class="_51m-"></td> 
           </td> 
          </tr> 
         </tbody> 
        </table> 
       </div> 
      </div> 
     </div> 
    </body> 
    </html> 
</iframe> 
+0

請發佈按鈕的相關HTML和其他你認爲有用的東西。 – JeffC

+0

@JeffC,我添加了HTML代碼 – Ragnarsson

+0

當您發佈HTML時,請花一點時間使用像http://jsbeautifier.org/這樣的美化工具來正確格式化它。它使得它更容易閱讀,這使得你的問題更可能得到解答。謝謝! – JeffC

回答

0

也許你的XPath是沒有找到IframeFacebook,但找到的按鈕。第一次點擊是設置焦點。

嘗試添加另一個點擊以查看是否屬於這種情況。如果嘗試使用標記名稱或ID定位器作爲Iframe。

如果你希望你的生活更容易嘗試:

https://addons.mozilla.org/en-Us/firefox/addon/element-locator-for-webdriv/

而且,不講課,但它是使用XPath定位在萬不得已一般的最佳實踐。

  1. ID
  2. 標籤名稱
  3. 文本
  4. HREF
+0

感謝您的幫助,但在我們使用的插件中,沒有ID或任何易於找到的相同標籤。我添加了插件的HTML – Ragnarsson

0

我已經驗證它。它不在iframe中。

使用下面的邏輯

String postNo="1"; // To click on share of first post in timeline 
driver.findElement(By.xpath("//div[contains(@id,'substream')]["+postNo+"]//span[@class='share_root']//span[text()='Share']")).click(); 
driver.findElement(By.xpath("//div[contains(@id,'substream')]["+postNo+"]//span[@class='share_root']//span[text()='Share']")).click(); 

//click on =>(Share Now friends) with below locator 
By.xpath("//ul[@role='menu']//span[text()='Share Now (Friends)']") 

//click on =>Share... 
By.xpath("//ul[@role='menu']//span[text()='Share…']") 

編輯-I

如果我們在分享鏈接點擊一次它是不開放以下選項(不知道爲什麼?!)。嘗試點擊兩次。

+0

感謝您的幫助,但我沒有點擊FB本身,我們的web應用程序有一個按鈕共享,一個插件,當您點擊時,您必須登錄您的FB並共享。我添加了HTML代碼 – Ragnarsson