C# 控制台不';不要写任何值

C# 控制台不';不要写任何值,c#,selenium,web-scraping,C#,Selenium,Web Scraping,我正在努力清理books.toscrape.com一切似乎都很完美,但它并没有向控制台输出任何东西 我确信XPath是正确的,语法也是正确的 我没有看到任何错误或警告 我不知道该怎么解决这个问题 using System; using System.Windows; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using System.Data; using System.Collections.Generic; namespac

我正在努力清理
books.toscrape.com
一切似乎都很完美,但它并没有向控制台输出任何东西

我确信XPath是正确的,语法也是正确的

我没有看到任何错误或警告

我不知道该怎么解决这个问题

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();

        }


    }

}
使用系统;
使用System.Windows;
使用OpenQA.Selenium;
使用OpenQA.Selenium.Firefox;
使用系统数据;
使用System.Collections.Generic;
名称空间书本刮削
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
DataTable=新的DataTable();
公共主窗口()
{
初始化组件();
}
字符串用户地址;
课堂用书
{
公共字符串标题{get;set;}
公共字符串Price{get;set;}
公共字符串速率{get;set;}
}
公共空间刮擦()
{
var books=新列表();
IWebDriver=新的FirefoxDriver();
user_url=Textbox1.Text;
int.TryParse(Textbox2.Text,输出变量x);
对于(int i=1;i
我希望输出的标题,价格,价格像文字一样
hello world 50 three,或类似的内容

您的图书列表为空,请尝试此选项

书籍。添加(新书)
{
Titlex=driver.FindElement(By.XPath(“//h3/a”)).Text,
Price=driver.FindElement(By.XPath(//p[@class='Price\u color'])。文本,
Rate=driver.FindElement(By.XPath(“//article/p”).GetAttribute(“class”)?.Replace(“星级”)和,
});
而不是:

var book=新书
{
Titlex=driver.FindElement(By.XPath(“//h3/a”)).Text,
Price=driver.FindElement(By.XPath(//p[@class='Price\u color'])。文本,
Rate=driver.FindElement(By.XPath(“//article/p”).GetAttribute(“class”)?.Replace(“星级”)和,
};

这看起来不像是一个控制台应用程序。但是您什么时候将
.Add()
添加到
书籍
列表中?它已声明但未使用。您没有向
书籍添加任何书籍。。。另外,您的第三个循环可能也需要移到外部…否则,没有理由创建一个列表,您将循环通过该列表打印到控制台。