C # SELENIUM - How to read elements of a webpage by keyword mention

3

I'm creating a Bot that searches a word in google and writes to a txt file all the links found in the search

I'm currently trying to find a keyword on the search result page, I store the links that contain that keyword in a variable and save it in txt:

 string mencoesDaPalavra =  Driver.FindElements(By.PartialLinkText("Palavra a pesquisar")).ToString();

After "capturing" the links that contain the word I'm trying to write to the file like this:

StreamWriter file = new StreamWriter("new.txt");
        file.Write(mencoesDaPalavra);
        file.Close();

My attempt was not successful, the line recorded in the file does not match what I'm looking for .. how could I do it?

line written to file:

  

new.txt =    "System.Collections.ObjectModel.ReadOnlyCollection'1 [OpenQA.Selenium.IWebElement]"

    
asked by anonymous 17.02.2016 / 14:45

1 answer

0

I do not know if I understand your problem well ...

Throw the result of your findElements into a collection and make a for each in it.

In each collection item use GetAttribute ("href") to get the link

I'll do a different way to get the elements, but you can use your own way. Then he says it worked, or I traveled. Rsrs

    var colEl = driver.findElements(By.Xpath("//a")).Where(el => el.Text.contains(palavraPesquisa));

    StreamWriter file = new StreamWriter("new.txt");

    foreach (var link in colEl)
            {
                file.Write(link.GetAttribute("href"));
                file.Close();
            }
    
12.05.2016 / 21:04