使用 Scrapy (WebScraping) 点击 HTML 个元素

Click on HTML elements with Scrapy (WebScraping)

我正在使用 scrapySharp 或 HtmlAgilityPack 用 c# 编写一个程序。但是我的缺点是我需要的那部分信息会在我单击 HTML 元素(按钮,link)时出现。

在某些论坛上有人评论说使用 Selenium 时可以操纵 html 元素,因此我尝试了以下方法

    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;

    // Defines the interface with the Chrome browser
    IWebDriver driver = new ChromeDriver ();
    // Auxiliary to store the label element in href
    Element IWebElement;
    // Go to the website
    driver.Url = url;

    // Click on the download button
    driver.FindElement (By.Id ("Download button")). Click ();

但作为一个网络自动化测试,它会打开浏览器和网站来执行选择过程(点击),所以它不是我使用的,因为我必须在内部对几个网站进行检查。

虽然我可以继续使用 Selenium,但我正在寻找避免使用浏览器的方法,而是在没有它的情况下单击。 有谁知道如何实现点击 link 或按钮,而不需要打开浏览器进行网页抓取?

希望对有相同需求的人有所帮助。
如果您想避免打开浏览器,可以在 ChromeDriver 中使用以下设置。

// settings for avoid opening browser
var options = new ChromeOptions();
options.AddArgument("headless");
var service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;

// url to access and scrape
var url = "https://example.com";

using (var driver = new ChromeDriver(service, options))
{
    // access the url
    driver.Navigate().GoToUrl(url);

    // Click on the download button - copied from your code above
    driver.FindElement (By.Id ("Download button")). Click (); 
}

除了上面还有下面的链接,你可能会觉得有用,

can-selenium-webdriver-open-browser-windows-silently-in-background

running-webdriver-without-opening-actual-browser-window