2013-01-16 26 views
5

我正在嘗試使用Webdriver點擊Facebook Like按鈕。你可以看到一個例子像按鈕at this page如何點擊使用Webdriver的Facebook Like按鈕

切換後的iframe我已經試過:

page.execute_script("document.querySelector('.pluginConnectButton > div:first-child button').click()") 

這個腳本切換到IFRAME後的作品在Firebug和Chrome開發者工具。

不過,這並不在FirefoxDriver和ChromeDriver工作(腳本傳遞,但按鈕不會更改爲點擊一個)

我如何可以點擊使用webdriver的這個按鈕?

+0

您可以顯示工作的例子嗎? –

+0

@MurtazaHussain在[本頁](http://developers.facebook.com/docs/reference/plugins/like/)上可以找到Like按鈕。我需要在另一個頁面點擊按鈕,但按鈕是一樣的。 –

+0

我能夠得到這個在watir-webdriver中工作,但我必須在點擊按鈕之前觸發按鈕的mouseover事件。不幸的是,我無法弄清楚如何在Capybara或Selenium-Webdriver中複製相同的內容。 Facebook可能也會檢查webdriver的使用,就像您在iframe中看到的那樣,它會看到一些webddriver屬性等於true。 –

回答

4

雖然這對我來說似乎很脆弱。我不得不使用一些JavaScript來獲取iframe的id,因爲FB會在每次頁面加載時更改id。幸運的是,他們並沒有改變iframe的類。

id = page.evaluate_script("$('.fb_ltr').attr('id');") 
within_frame(id) do 
    page.first(:css, '.pluginButton').click 
end 
0

這個爲你工作。

if(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Like[\\s\\S]*$")== true) { 
driver.findElement(By.xpath("//form[@id='u_0_1']/div/div/div/button")).click(); 
} 
else{ 
System.out.println("Unable to click Like button on page"); 
} 
0

我正在使用頁面對象gem。這裏是我的方法,點擊喜歡FB按鈕:

in_frame(:class =>"fb_ltr") do |frame| 
    button(:fb_btn, :css => "#facebook div.pluginButton.pluginButtonSmall.pluginButtonInline.pluginConnectButtonDisconnected button" , :frame => frame) 
    end 

...

fb_btn_element.when_present.click