2015-08-26 24 views
-2
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() 
    { 
    } 
} 
+0

阐述你的问题? –

+0

我想使用selenium webdriver在Facebook上发表评论,但我无法找到评论框,我试图找到使用xpath和类,但无法做到这一点 –

+0

生成'?209g'和'_2vxa'类名每一次,所以你不会在页面刷新后得到相同的类。 – peetya

回答

-1

试试这个 dv.findElement(By.xpath( “//一个[@类= 'UFILikeLink']”)),以使在Facebook发布评论点击(); Thread.sleep(2000); dv.findElement(By.xpath(“// a [@ class ='comment_link']”))。click(); dv.findElement(By.className(“_ 54-z”))。sendKeys(“hgfghjkj”); Thread.sleep(2000);dv.findElement(By.className(“_ 54-z”))。sendKeys(Keys.RETURN);

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来杀死某人。

+0

请编辑你的答案并删除所有的诅咒。它*可能公平地表达你对该API的看法,但请记住:这是一个通用的q/a网站;而且您的内容将来可能会被很多人阅读。因此:请编辑! – GhostCat