Selenium Webdriver Automated login

2019-09-13 20:36发布

I have selenium webdriver and am using C# to login and post a random message to facebook, linkedin and twitter. I cannot click the post button on linkedin and facebook. Please Help

using (IWebDriver driver = new ChromeDriver(opt))

        {

            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("https://www.facebook.com");
            Console.WriteLine("Opened Facebook");

            driver.FindElement(By.Id("email")).SendKeys("userName");
            Console.WriteLine("username entered");

            driver.FindElement(By.Id("pass")).SendKeys("password");
            Console.WriteLine("password entered");

            driver.FindElement(By.Id("u_0_q")).Click();
            Console.WriteLine("Details Validated");

            driver.FindElement(By.Name("xhpc_message")).Click();
            driver.FindElement(By.Name("xhpc_message")).SendKeys("Message");
            Console.WriteLine("Message has been wrritten");
            //driver.FindElement(By.Name("xhpc_message")).SendKeys(OpenQA.Selenium.Keys.Return);
            driver.FindElement(By.CssSelector("#js_2en > div._1j2v > div._2dck._4-u3._57d8 > div > div._ohf.rfloat > div > button > span > em")).Click();

            Console.WriteLine("Message posted");
        }

        using (IWebDriver driver2 = new ChromeDriver(opt))
        {

            driver2.Navigate().GoToUrl("https://www.linkedin.com");
            driver2.FindElement(By.Id("login-email")).SendKeys("userName");
            driver2.FindElement(By.Id("login-password")).SendKeys("Passsword");
            driver2.FindElement(By.Id("login-submit")).Click();
            driver2.FindElement(By.CssSelector("#ember4031 > div > div.sharing-create-share-view__create-content.sharing-create-share-view__create-content--is-default.rounded-button-theme.rounded-button-theme-b > button")).Click();
            driver2.FindElement(By.CssSelector("#ember3148")).SendKeys("Automated Message using Selenium");
            driver2.FindElement(By.Id("ember3113"));

        }

        using (IWebDriver driver3 = new ChromeDriver(opt))
        {

            driver3.Navigate().GoToUrl("https://twitter.com/?lang=en-GB");
            driver3.FindElement(By.CssSelector("#doc > div.StreamsTopBar-container.StreamsTopBar-container--withStreamHero.StreamsTopBar-container--withTallHeader > div > div.StreamsHero.StreamsHero--tall > div.StreamsHero-buttonContainer > a.Button.StreamsLogin.js-login")).Click();
            driver3.FindElement(By.CssSelector("#login-dialog-dialog > div.LoginDialog-content.modal-content > div.LoginDialog-body.modal-body > div.LoginDialog-form > form > div.LoginForm-input.LoginForm-username > input")).SendKeys("userName");
            driver3.FindElement(By.CssSelector("#login-dialog-dialog > div.LoginDialog-content.modal-content > div.LoginDialog-body.modal-body > div.LoginDialog-form > form > div.LoginForm-input.LoginForm-password > input")).SendKeys("Password");
            driver3.FindElement(By.CssSelector("#login-dialog-dialog > div.LoginDialog-content.modal-content > div.LoginDialog-body.modal-body > div.LoginDialog-form > form > input.submit.btn.primary-btn.js-submit")).Click();
            driver3.FindElement(By.CssSelector("#tweet-box-home-timeline")).SendKeys("Message");
            driver3.FindElement(By.CssSelector("#timeline > div.timeline-tweet-box > div > form > div.TweetBoxToolbar > div.TweetBoxToolbar-tweetButton.tweet-button > button")).Click();

        }

1条回答
时光不老,我们不散
2楼-- · 2019-09-13 21:21

Your question requires more information, but there are a number of different ways to select elements with Selenium. You may try something like this for Facebook:

 _driver.FindElement(By.XPath("//div[1]/span[1]/a/span[1][contains(text(), 'Create a Post')]")).Click();
 var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(5));
 wait.Until(ExpectedConditions.ElementExists(By.XPath("//*[@id='composer_text_input_box']")));
 _driver.FindElement(By.XPath("//*[@id='composer_text_input_box']/div/div/div[2]/div")).Click();
 _driver.FindElement(By.XPath("//*[@id='composer_text_input_box']/div/div/div[2]/div")).SendKeys("Test Message");
 _driver.FindElement(By.XPath("//div[2]/div[3]/div/div[2]/div/button/span/em[contains(text(), 'Post')]")).Click();

For more information: https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_By.htm

查看更多
登录 后发表回答