0

Hello i'm trying to automate Facebook messages so i can send automatic message to a specific friend. The first thing i did is to connect to my account and everything works fine except when i try to locate a friend within the chat side bar .

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace Automation
{
    class Program
    {
        static void Main(string[] args)
        {
            using (IWebDriver driver = new FirefoxDriver())
            {
                driver.Navigate().GoToUrl("https://facebook.com");

                IWebElement username = driver.FindElement(By.XPath(".//input[@id='email']"));
                username.SendKeys("username");

                IWebElement password = driver.FindElement(By.XPath(".//input[@id='pass']"));
                password.SendKeys("password");

                IWebElement submit = driver.FindElement(By.XPath(".//input[@id='u_0_l']"));
                submit.Submit();

                var wait = new WebDriverWait(driver,TimeSpan.FromSeconds(20));
                wait.Until(d => d.Title == "Facebook");
              // Problem goes here
                IWebElement friend = driver.FindElement(By.XPath(".//a[@href='/messages/id']"));
                friend.Click();

                var waitFriendLoad = new WebDriverWait(driver,TimeSpan.FromSeconds(10));
                waitFriendLoad.Until(d => d.Url == "https://facebook.com//messages/id");
            }
        }
    }
}

Everything works fine except when i try locate the friend using x-path. So, it throws Unable to locate element exception

Anuraj R
  • 524
  • 2
  • 8
  • 24
D.AhmedRafik
  • 81
  • 2
  • 15
  • I can't see anything specific in your XPath to say "access my friend with ID=n", for example. After loggin in I did a test with a cssSelector like this - driver.FindElement(By.CssSelector("div._55lr")) -> this returns a list with all of your friends that appear in the right bar to chat. You could manipulate them. And, if you can, please consider changing XPath for CssSelector (it's shorter and has less chances to change if the site is redesigned). – Tom Oct 15 '16 at 15:28
  • 1
    See http://stackoverflow.com/questions/6992993/selenium-c-sharp-webdriver-wait-until-element-is-present – MikeJRamsey56 Oct 15 '16 at 21:02
  • Thank you @MikeJRamsey56 – D.AhmedRafik Oct 17 '16 at 08:58

0 Answers0