0

How to Post Status in Facebook using Selenium and Java?I have tried below code which doesn't work.Able to login but getting error no such element while posting status.After login i am getting notification popup allow or block,how to handle this popup also? Below the code i am using for testing this.

public class NewTest {
private WebDriver driver;

 @Test
public void testEasy() throws InterruptedException {

driver.get("https://www.facebook.com/");
Thread.sleep(5000);
driver.findElement(By.id("email")).sendKeys("email");
driver.findElement(By.id("pass")).sendKeys("password" + Keys.ENTER);

Thread.sleep(5000);

driver.findElement(By.xpath("//textarea[@title=\"What's on your mind?\"]")).click();
driver.findElement(By.xpath("//textarea[@title=\"What's on your mind?\"]")).sendKeys("Hello World");
driver.findElement(By.xpath("//textarea[@title=\"What's on your mind?\"]")).sendKeys(Keys.ENTER);

}

@BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\admin\\Desktop\\Test\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
}

@AfterTest
public void afterTest() {
driver.quit();
}

}

user1984
  • 13
  • 2
  • 11

1 Answers1

0
  1. Your XPath expression is not very correct, as far as I can see thte title of the relevant textarea looks like:

    What's on your mind, user1984
    

    so you need to amend your locator to use XPath contains() function like:

    By.xpath("//textarea[contains(@title,\"What's on your mind\")]")
    
  2. Using Thread.sleep is a performance anti-pattern, you should be using WebDriverWait instead. Example refactored code:

    driver.get("https://www.facebook.com/");
    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.elementToBeClickable(By.id("email"))).sendKeys("email");
    wait.until(ExpectedConditions.elementToBeClickable(By.id("pass"))).sendKeys("password" + Keys.ENTER);
    
    
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//textarea[contains(@title,\"What's on your mind\")]"))).click();
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//textarea[contains(@title,\"What's on your mind\")]"))).sendKeys("Hello World");
    
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks.But i am getting same error when i tried above code.Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//textarea[contains(@title,"What's on your mind\")]"} – user1984 Jun 26 '19 at 09:37