如何使用selenium对facebook帖子发表评论

时间:2015-08-26 12:16:50

标签: java selenium

public class testngprj {
  public String baseurl="https://www.facebook.com/"; 
  public WebDriver dv= new FirefoxDriver(); 

  @Test (priority=0) public void gettitleverified() { 
    String expectedTitle="Facebook - Log In or Sign Up"; 
    String actualtitle=dv.getTitle(); 
    AssertJUnit.assertEquals(expectedTitle, actualtitle); 
  } 

  @Test (priority=1) public void validlogin() {
    dv.findElement(By.id("email")).sendKeys("username");
    dv.findElement(By.id("pass")).sendKeys("pass");
    dv.findElement(By.id("loginbutton")).click();
  } 

  @Test (priority=2) public void makecomment() {
   //JavascriptExecutor jse = (JavascriptExecutor)dv;  
   //jse.executeScript("window.scrollBy(0,2000)", ""); 
    dv.findElement(By.xpath("/html/body/div[1]/div[1]/div/div/div/div[1]/div/div/div[2]/ul/li[1]/a/span")).click();
    dv.findElement(By.className("_209g _2vxa")).sendKeys("Nice one");
    dv.findElement(By.className("_209g _2vxa")).sendKeys(Keys.ENTER);
  } 

  @BeforeTest public void beforeTest() { 
  dv.get(baseurl); 
  }

   @AfterTest public void afterTest() 
  { 
  } 
}

1 个答案:

答案 0 :(得分:0)

最后我做到了!在python中检查这段代码。

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options

firefox_options = Options()
firefox_options.add_argument('--dns-prefetch-disable')
firefox_options.add_argument('--no-sandbox')
firefox_options.add_argument('--lang=en-US')
browser = webdriver.Firefox(executable_path='/home/coder/Documents/Projects/socialbot/geckodriver', firefox_options=firefox_options)


browser.get('https://www.facebook.com/')
signup_elem = browser.find_element_by_id('email')
signup_elem.send_keys('EMAILHERE')

login_elem = browser.find_element_by_id('pass')
login_elem.send_keys('PASSHERE')

ins = browser.find_elements_by_tag_name('input')
for x in ins:
    if x.get_attribute('value') == 'Log In':
        x.click() # here logged in
        break
#then key here move to mobile version as that doesn't support javascript
browser.get('https://m.facebook.com')
el = browser.find_element_by_name('query')
el.send_keys('antony white')
el.send_keys(Keys.ENTER)
sleep(3)

temp= ''
ak = browser.find_elements_by_tag_name('a')
for a in ak:
    if a.get_attribute('href').endswith('search'):
        a.click()
        temp = a.get_attribute('href')[:a.get_attribute('href').find("?")]
        break

# CLICK TIMELINE
browser.get(temp+'?v=timeline')
sleep(10)


# find last post (occurance of comment)
as_el = browser.find_elements_by_tag_name('a')
for a in as_el:
    print(a.text)
    if 'omment' in a.text.strip():
        a.click()
        break
sleep(10)


# do actual comment
ins = browser.find_element_by_id('composerInput')
ins.send_keys('Best cars !')
# submit input
ins = browser.find_elements_by_tag_name('input')
for x in ins:
    if 'omment' in x.get_attribute('value'):
        x.click()
        break

移动到手机facebook版本救了我的命。最后。我尝试用facebook api做到这一点。但他们的Api真的很糟糕!我做了,浪费了很多时间在他们的图表api上,那是一场灾难。而且我认为facebook想用他们的图表api杀死某人。