简体   繁体   English

用selenium java单击Facebook“共享”按钮

[英]Click Facebook 'share' button with selenium java

I have to test this FB buttons: 我必须测试以下FB按钮:

在此处输入图片说明

Facebook buttons are inside an iframe. Facebook按钮位于iframe中。 I switch to iframe, and I am able to find 'Share' button. 我切换到iframe,可以找到“共享”按钮。 But when webDriver clicks on it, the popup window doesn't show up. 但是当webDriver单击它时,不会显示弹出窗口。 My code is: 我的代码是:

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();

I do the same for Twitter buttons, and it works. 我对Twitter按钮也做同样的事情,并且可以正常工作。 What is special about FB buttons? FB按钮有何特别之处?

EDIT 编辑

The HTML code of FB buttons: 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>

Maybe your xpath isn't finding the IframeFacebook, but is finding the button. 也许您的xpath找不到IframeFacebook,但是找到了按钮。 The first click is setting the focus. 第一次单击将设置焦点。

Try adding another click to see if this is the case. 尝试添加另一个单击以查看是否是这种情况。 If it is try using Tag Name or ID locator for the Iframe. 如果是这样,请尝试为iframe使用代码名称或ID定位器。

If you want your life to be easier try: 如果您希望生活更轻松,请尝试:

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

Also, not to lecture, but it is general best practice to use Xpath locators as a last resort. 另外,不讲课,但通常最好的方法是使用Xpath定位器。

  1. ID ID
  2. Tag Name 标签名称
  3. Text 文本
  4. Href HREF

I've verified it. 我已经验证了 It is not in iframe. 它不在iframe中。

Use below logic 使用以下逻辑

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…']")

EDIT-I 编辑-I

If we click on Share link once it is not opening below options (No idea why?!). 如果我们单击“共享”链接,但该链接没有在选项下方打开(不知道为什么?!)。 Try to click twice. 尝试单击两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM