如何使用selenium webdriver发送facebook消息

时间:2016-02-05 05:42:47

标签: facebook selenium webdriver message

我可以在facebook上登录,能够打开聊天但无法发送任何消息 我使用的程序代码如下:

//登录FB>> 工作正常

driver.findElement(By.xpath(".//*[@id='email']")).sendKeys("******@gmail.com"); 
driver.findElement(By.xpath(".//*[@id='pass']")).sendKeys("********");
driver.findElement(By.xpath(".//*[@id='u_0_l']")).click();

//点击留言图标>> 工作正常

 driver.findElement(By.xpath(".//*[@id='u_0_h']/li[1]/div/a/span")).click();

//点击朋友姓名,我要向其发送消息>> 工作正常

driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/div/div/div[1]/div/div/div[2]/ul/li[5]/div/div[2]/div/div[3]/div/div[1]/div/div/ul/li[2]/a")).click();
Thread.sleep(5000);

//发送消息>>>>在这里,我没有得到任何回复,代码运行没有输入任何消息或错误

driver.findElement(By.xpath(".//*[@class='_552h _35li _n4k']")).sendKeys("Hiii");
driver.findElement(By.xpath("/html/body/div/div[5]/div[1]/div/div/div[1]/div/div[1]/div[2]/div/div/div/div/div[4]/div[5]/div[1]/div/div/div[2]/div/div/div")).sendKeys(Keys.ENTER);;

3 个答案:

答案 0 :(得分:2)

require 'selenium-webdriver'
@driver = Selenium::WebDriver.for :chrome
@driver.get 'https://www.facebook.com/fname.lname?fref=none'
a = @driver.find_element(:xpath, '//[@id="email"]').send_keys('aaa@gmail.com')
a = @driver.find_element(:xpath, '//*[@id="pass"]').send_keys('12345678')
a = @driver.find_element(:xpath, '//*[@value="Log In"]').click
sleep 5
a = @driver.find_element(:xpath, '//a[@href="/messages/fname.lname" and @role="button"]').click
sleep 2;p 'This is Where I clicked/initiated the Send Message '
a = @driver.find_element(:xpath, '//div[@class="_1ia"]/descendant::div[@class="_5rpu" and @role="textbox"]')
a.send_keys('Hi There') # This is where I entered the keys and Did Enter
a.send_keys:enter

**Screen shot**

答案 1 :(得分:2)

我使用过此代码,对我有用。

driver.findElement(By.xpath("//div[contains(@class,'_5rpu') and @role='combobox']")).sendKeys("hi"+Keys.ENTER);

enter image description here

答案 2 :(得分:0)

@role更改后的正确答案是:

WebElement sendmsg = driver
            .findElement(By.xpath("//div[@class='_1ia']/descendant::div[@class='_5rpu' and @role='combobox']"));
sendmsg.sendKeys("Just testing: using selenium webdriver" + Keys.ENTER);