简体   繁体   中英

Click Facebook 'share' button with selenium java

I have to test this FB buttons:

在此处输入图片说明

Facebook buttons are inside an iframe. I switch to iframe, and I am able to find 'Share' button. But when webDriver clicks on it, the popup window doesn't show up. 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. What is special about FB buttons?

EDIT

The HTML code of FB buttons:

<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. 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.

If you want your life to be easier try:

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.

  1. ID
  2. Tag Name
  3. Text
  4. Href

I've verified it. It is not in 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

If we click on Share link once it is not opening below options (No idea why?!). Try to click twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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