Skip to content

Instantly share code, notes, and snippets.

@afandilham
Last active May 6, 2020 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afandilham/8323c15136700f73367dadf8bacff084 to your computer and use it in GitHub Desktop.
Save afandilham/8323c15136700f73367dadf8bacff084 to your computer and use it in GitHub Desktop.
Simple web scraping using Selenium Web Driver
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace Web_Scraping
{
public partial class Form1 : Form
{
public IWebDriver driver;
public void Buka_browser()
{
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
driver = new ChromeDriver(driverService);
try
{
driver.Navigate().GoToUrl("https://www.bukalapak.com/products?utf8=%E2%9C%93&source=navbar&from=omnisearch&search_source=omnisearch_organic&from_keyword_history=false&search%5Bkeywords%5D=tas");
}
catch
{
throw;
}
}
private void Cari_data()
{
IList<IWebElement> searchElements = driver.FindElements(By.ClassName("product-description"));
foreach (IWebElement i in searchElements)
{
HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
var text = i.GetAttribute("innerHTML");
htmlDocument.LoadHtml(text);
var Inputs = htmlDocument.DocumentNode.Descendants("h3").ToList();
foreach (var items in Inputs)
{
HtmlAgilityPack.HtmlDocument htmlDocument1 = new HtmlAgilityPack.HtmlDocument();
htmlDocument1.LoadHtml(items.InnerHtml);
var tds = htmlDocument1.DocumentNode.Descendants("a").ToList();
if (tds.Count != 0)
{
text_box.AppendText(tds[0].InnerText + " " + "\t\r");
}
}
text_box.AppendText("\t\r");
}
}
public Form1()
{
InitializeComponent();
}
private void Label1_Click(object sender, EventArgs e)
{
}
private void Button2_Click(object sender, EventArgs e)
{
Buka_browser();
}
private void Button3_Click(object sender, EventArgs e)
{
if (driver != null)
{
driver.Quit();
}
}
private void Button1_Click(object sender, EventArgs e)
{
Cari_data();
}
private void RichTextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment