Selenium无法使用Xpath定位元素

时间:2016-10-15 12:50:18

标签: c# selenium xpath selenium-webdriver

您好我正在尝试自动发送Facebook消息,以便我可以向特定朋友发送自动消息。 我做的第一件事是连接到我的帐户,一切正常,除非我试图在聊天侧栏中找到一个朋友。

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

除非我尝试使用x-path找到朋友,否则一切正常。因此,它抛出无法定位元素异常

0 个答案:

没有答案