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



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

driver.findElement(By.name("xhpc_message_text"((.sendkeys("Hello World"(; WebDriverWait = new WebDriverWait(driver, 5(;

试试下面的代码..它必须为你工作..

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();
}
}

我已经找到了解决您问题的方法。请尝试以下代码:

法典:

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");

解释:

首先,您必须单击共享更新文本框,然后在内容可编辑设置为true的div 元素上发送密钥。

最新更新