如何提取某些链接并写入文本文件

时间:2017-02-08 22:56:00

标签: c# selenium

我是C#的新手,特别是在Selenium。我提供的代码按预期工作,但我想补充一下。基本上如何使用Selenium登录Linkedin,搜索公司的当前员工(本例中是沃尔玛)并搜索"发送inmail"的链接。对于每个用户....点击" next"每个页面上的按钮,直到没有更多。以下将打开linkedin,登录,输入walmart进入搜索框,然后单击下一步按钮。

我想将所有Send InMail按钮中找到的链接写入文本文件。检查其中一个Send InMail按钮提供以下内容:

 <a class="primary-action-button label" href="/requestList?displayProposal=&amp;destID=262919732&amp;creationType=DC&amp;authToken=BrmS&amp;authType=name&amp;trk=vsrp_people_res_pri_act&* amp;trkInfo=VSRPsearchId%3A5225861601486589992400%2CVSRPtargetId%3A262919732%2CVSRPcmpt%3Aprimary">Send InMail</a>

我想将所有这些链接写入文本文件,因为它循环遍历所有&#34; next&#34;纽扣。此外,我想知道如何从下拉列表中选择一个我无法检查&#34;&#34; ...当你输入&#34; Walmart&#34;或者在搜索框中的任何内容,下拉菜单为您提供了选择&#34;目前在沃尔玛&#34;等工作的人的选项。我甚至无法在开发者模式中检查该选项某些原因。 我已经更新了我的代码......现在看来想要做我需要的东西......但似乎有一个时间问题,其中&#34; next&#34;按钮可能会在&#34; Send InMail&#34;之前加载。按钮...它会向控制台打印一些结果,然后点击几下,但随后似乎融化了:

// Go to the home page
            driver.Navigate().GoToUrl("https://www.linkedin.com");



            // Get User Name field, Password field and Login Button
            var userNameField = driver.FindElementById("login-email");
            var userPasswordField = driver.FindElementById("login-password");
            var loginButton = driver.FindElementByXPath("//input[@value='Sign in']");

            // Type user name and password
            userNameField.SendKeys("me@hotmail.com");
            userPasswordField.SendKeys("Password123");

            // and click the login button
            loginButton.Click();

            // perform search
            var newSearch = driver.FindElementById("main-search-box");
            var searchButton = driver.FindElementByName("search");

            // search
            newSearch.SendKeys("walmart");
            searchButton.Click();

            // Get all links from Send InMail buttons
            List<IWebElement> elementList = new List<IWebElement>();
            elementList.AddRange(driver.FindElements(By.LinkText("Next >")));
            if (elementList.Count > 0)
            {
                foreach(IWebElement item in driver.FindElements(By.LinkText("Send InMail")))
                {

                    Console.WriteLine(item.GetAttribute("href"));
                    var goForward = driver.FindElementByLinkText("Next >");
                    goForward.Click();

                }
            }


            Console.ReadLine();

1 个答案:

答案 0 :(得分:0)

据我了解,在您执行搜索后,搜索结果会在列表中填充,如附图所示。

screenshot of the Linkedin search result.

然后使用类似的方法迭代结果(此处的代码示例在Java中,在C#中可能类似)

List<WebElements> results_div = driver.findElemnts(By.xpath("//*[@id="results"]")) // where xpath of the <ul> element

int counter = 2 // because the data starts from id = 2. Refer image.
while(counter <= results_div)
{
     WebElement element = driver.findElements(By.xpath("//*[@data-li-position=\"+counter+\"])) //xpath of the <li> element
     String anchor_text = element.findElement(By.linkText("Send InMail")).getAtribute("href")

    //Write a logic to save the data to a text file 
}

迭代上述内容,直到达到所有结果!

希望它有所帮助。

编辑:试试这个,它不是一个有效的代码!但是试试这个理由。这可能有所帮助。

@Test(description = "Search the Site with some predefined words after Login and print the href attribute")
public void printUserID()
{
    StartPage startPage = new StartPage(driver);
    HomePage homePage = startPage.loginIntoAccount(LinkdinAccount.linkedEmail,LinkdinAccount.linkedPassword); // Logs in in to the account

    driver.findElement(By.xpath("//*[@id=\"main-search-box\"]")).sendKeys("herbalife");
    driver.findElement(By.xpath("//*[@id=\"global-search\"]/fieldset/button")).click();

    WebDriverWait wait = new WebDriverWait(driver, 60);
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@id=\"results\"]"))));

    List<WebElement> results_div = driver.findElements(By.xpath("//*[@id=\"results\"]/li")); // where xpath of the <ul> element
    System.out.println(results_div.size()); //*[@id="results"]

    #driver.findElement(By.xpath("//*[@id=\"results-pagination\"]/ul/li[11]/a")).click();

    int count = 1;

         wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@id=\"results\"]"))));

    while(driver.findElement(By.xpath("//*[@id=\"results-pagination\"]/ul/li[11]/a")).isDisplayed())
    {
       while(count <= results_div.size())
       { 
        WebElement element = driver.findElement(By.xpath("//*[@data-li-position=\"" + count + "\"]"));


        if(element.findElements(By.linkText("Send Inmail")).size() > 0)
        {
            String anchor_text = element.findElement(By.linkText("Send InMail")).getAttribute("href");
            System.out.println(anchor_text);
        }

        count ++;
        }
    //for clicking the next button 
    driver.findElement(By.xpath("//*[@id=\"results-pagination\"]/ul/li[11]/a")).click()
    }