Jordan
Jordan

Reputation: 3

Is there a way (in c#) to iterate through article elements on a website's HTML?

I'm trying to iterate through the elements on a page, and they are all categorized under the html <article> keyword. How can I do this?

<article>
 <div class="inner-article">
  <a style="height:150px;" href="/shop/jackets/v87vh6cpt/rnv032l4i">
   <img width="150" height="150" 
   src="//assets.supremenewyork.com/179529/vi/4DVL66YDwcs.jpg" 
   alt="4dvl66ydwcs"> 
   <div class="sold_out_tag">sold out</div>
  </a>
<h1>
 <a class="name-link" 
 href="/shop/jackets/v87vh6cpt/rnv032l4i">Supreme®/Honda®/Fox® Racing Puffy 
 Zip Up Jacket</a>
 </h1>
  <p>
   <a class="name-link" href="/shop/jackets/v87vh6cpt/rnv032l4i">Black</a>
  </p>
 </div>
</article>

In a nutshell, I need to iterate through a bunch of elements on a page (with HTML code listed above) and use keywords to test the keywords against the innerHTML: Supreme®/Honda®/Fox® Racing Puffy Zip Up Jacket. Keywords are like: Honda, Fox, and Puffy.

If an element matches 2 or more keywords, then it clicks on the element.

do
            {
                driver.Navigate().Refresh();
                try
                {
                    driver.FindElement(By.LinkText("Breed Crewneck"));
                    elementFound = true;
                }catch(NoSuchElementException error)
                {
                    Console.WriteLine("No such element found!");
                }
            } while (elementFound == false);

This is my current code. It only looks through the page for a LinkText element. This is NOT viable, as it requires a specific string; the LinkText option cannot use keywords. Additionally, it is a true/false conditional statement. I need to implement iteration.

Upvotes: 0

Views: 924

Answers (3)

JeffC
JeffC

Reputation: 25645

I would store the list of keywords in a string array. Then grab each A tag under each ARTICLE tag (that holds the product name, e.g. "Supreme®/Honda®/Fox® Racing Puffy Zip Up Jacket") and then use LINQ to count the number of keywords within that string. If the count is >= 2, then click that element. The code is below.

string[] keywords = { "Honda", "Fox", "Puffy" };
foreach (IWebElement link in _driver.FindElements(By.CssSelector("article a.name-link")))
{
    if (keywords.Count(w => link.Text.Contains(w)) >= 2)
    {
        link.Click();
        break;
    }
}

Upvotes: 0

IPolnik
IPolnik

Reputation: 649

1) You r getting list of all links inside all articles

  protected IList<IWebElement> FindNestedElements()
            {
             return FindElement(By.XPath("your xpath for 
     class="name-link" element")).FindElements(By.XPath("your Xpath for article elements"));
            }

2) Once you collected all names inside all articles on the page u do foreach loop, inside for each loop first thing u r getting text of each element by element.Text, then u check for condition(compare if it contains text u need), if yes then click.

var values = new [] {"Honda", "Fox", "Puffy"};
foreach (IWebElement element in FindNestedElements())
            {
                if(values.Any(element.Text.Contains);)
                {
                    element.click;
                }

Upvotes: 1

yob
yob

Reputation: 528

Something like this?:

    var driveroptions = new ChromeOptions();
                driveroptions.AddUserProfilePreference("disable-popup-blocking", "true");

                using (IWebDriver driver = new ChromeDriver(this.SeleniumDriverPath, driveroptions))
                {
                    try
                    {
                        driver.Navigate().GoToUrl("http://www.google.com/");
                        IWebElement query = driver.FindElement(By.Name("q"));
                        query.SendKeys("link");
                        query.Submit();
                        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                        //
                        var listElement = driver.FindElements(OpenQA.Selenium.By.XPath(".//*[@id='search']//div[@class='g']"));
                        foreach( var e in listElement)
                        {
                            //include ctrl+click:
                            //var action = new OpenQA.Selenium.Interactions.Actions(driver);
                            //action.KeyDown(Keys.Control).Build().Perform();
                            //or click:
                            if ("Supreme®/Honda®/Fox® Racing Puffy Zip Up Jacket".Contains(e.Text))
                            {
                                e.Click();
                            }
                        }
                        //
                    }
                    driver.Quit();
                }

basically, you find your elements via XPath, :

    driver.FindElements(OpenQA.Selenium.By.XPath(".//*article//a[@class='name-link']"));

or by classname:

    driver.FindElements(OpenQA.Selenium.By.ClassName("name-link"))

iterate, and perform click based on condition?

Upvotes: 1

Related Questions