如何使用java和selenium webdriver发布facebook状态消息

时间:2017-07-04 15:23:25

标签: java facebook selenium

我登录了Facebook。现在,从主页或新闻Feed页面,我想在“共享更新”文本区域中键入一些消息,然后单击发布。 尝试在定位器下面。没用。

driver.findElement(By.name(“xhpc_message_text”))。sendkeys(“Hello World”); WebDriverWait wait = new WebDriverWait(driver,5);

2 个答案:

答案 0 :(得分:0)

我找到了问题的解决方案。请尝试以下代码:

<强>代码:

    url = "http://facebook.com";
    String email = "email";
    String password = "password";
    System.setProperty("webdriver.chrome.driver", "src/chromedriver 3");
    WebDriver driver = new ChromeDriver(); 
    driver.get(url);
    driver.manage().window().maximize();
    driver.findElement(By.id("email")).sendKeys("your email id");
    driver.findElement(By.id("pass")).sendKeys("Your password" + Keys.ENTER);
    WebDriverWait wait = new WebDriverWait(driver, 500);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//textarea[contains(@title,'Share an update')]")));
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//textarea[contains(@title,'Share an update')]")));
     driver.findElement(By.xpath("//textarea[contains(@title,'Share an update')]")).click();

     wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@contenteditable='true']")));
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@contenteditable='true']")));
   driver.findElement(By.xpath("//div[@contenteditable='true']")).sendKeys("Hello World");

说明:

首先,您必须点击共享更新文本框,然后在div元素上发送内容可编辑设置为 true <的键/强>

答案 1 :(得分:0)

尝试以下代码..它必须适合你..

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;



public class Facebook_login {

    public static void main(String[] args) throws InterruptedException {
        String user_name = "facebook_user_name";
        String pwd = "facebook_password";
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.facebook.com");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys(user_name);
        driver.findElement(By.name("pass")).clear();
        driver.findElement(By.name("pass")).sendKeys(pwd);
        driver.findElement(By.xpath("//input[contains(@value,'Log In')]")).click();
        Thread.sleep(10000);                
        System.out.println("logged in successfully");
        WebElement notification = driver.findElement(By.xpath("//a[contains(@action,'cancel')]"));
        if(notification.isDisplayed()) {
            System.out.println("Notification is present");
            notification.click();
        }
        WebElement status =driver.findElement(By.xpath("//textarea[@name='xhpc_message']"));
        status.sendKeys("Hello");
        Thread.sleep(3000);
        WebDriverWait wait = new WebDriverWait(driver,30);


        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Post']"))).click();

    }
}