首页 > 解决方案 > Console doesn't write any value

问题描述

I'm trying to scrape books.toscrape.com everything seems perfect, but it doesn't output anything to console.

I'm sure about the XPaths are correct, and syntax is right.

I don't see any errors, or warnings.

Don't have any clue what i can try for this problem.

using System;
using System.Windows;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Data;
using System.Collections.Generic;

namespace book_scraping
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {



        DataTable table = new DataTable();
        public MainWindow()
        {
            InitializeComponent();

        }

        string user_url;
        class Book
        {

            public string Titlex { get; set; }
            public string Price { get; set; }
            public string Rate { get; set; }


        }


        public void Scrape()
        {
            var books = new List<Book>();
            IWebDriver driver = new FirefoxDriver();
            user_url = Textbox1.Text;
            int.TryParse(Textbox2.Text, out var x);
            for (int i = 1; i < x; i++)
            {
                driver.Url = "http://" + user_url + "/catalogue/" + "page-" + i + ".html";
                var element = driver.FindElements(By.XPath("//article[@class='product_pod']"));
                foreach (var elements in element) {

                    var book = new Book
                    {
                        Titlex = driver.FindElement(By.XPath("//h3/a")).Text,
                        Price = driver.FindElement(By.XPath("//p[@class='price_color']")).Text,
                        Rate = driver.FindElement(By.XPath("//article/p")).GetAttribute("class")?.Replace("star-rating ", ""),
                    
                    };
                    foreach (var a in books)
                    {

                        Console.WriteLine($"{a.Titlex} {a.Price} {a.Rate}");

                   }

                }


            }

        }
    

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Scrape();

        }


    }

}

I expect the output of title,price,rate as text like hello world 50 three, or something similar

标签: c#seleniumweb-scraping

解决方案


你的书单是空的,试试这个

books.Add(new Book
{
   Titlex = driver.FindElement(By.XPath("//h3/a")).Text,
   Price = driver.FindElement(By.XPath("//p[@class='price_color']")).Text,
   Rate = driver.FindElement(By.XPath("//article/p")).GetAttribute("class")?.Replace("star-rating ", ""),

});

代替:

var book = new Book
{
   Titlex = driver.FindElement(By.XPath("//h3/a")).Text,
   Price = driver.FindElement(By.XPath("//p[@class='price_color']")).Text,
   Rate = driver.FindElement(By.XPath("//article/p")).GetAttribute("class")?.Replace("star-rating ", ""),

};

推荐阅读