![]() In our case, the condition is to wait until the new page has loaded. Var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)) However, this condition can prove to be a problem. Waiting itself is not an issue as we can use the WebDriverWait class that provides us with a way to wait a certain amount of time until an arbitrary condition happens. Note: We could just navigate to the URL instead of clicking on the side menu items, but I feel it’s better to demonstrate how to click and wait for the page to load as it is a pretty common problem in web scraping.Ĭlicking is easy – we find the element and call its Click method.ĭriver.FindElement(By.CssSelector("#side-menu > li.active > ul > li:nth-child(1) > a")).Click() Subsequently, we should click on the Laptops menu item and wait for the Laptops page to load. We can navigate by clicking on the Computers menu item and waiting for the Computers page to load. The next step is navigating – first to the Computers page and then to the Laptops page. Using (IWebDriver driver = new ChromeDriver(options)) ![]() To prevent this issue, we start the driver with some options where we specify that the browser should start maximized.ĬhromeOptions options = new ChromeOptions() That means that there’s a chance that the page will have a mobile/tablet layout so your CSS selectors (that are copied from the DevTools of a maximized browser window) will be invalid. Just a heads up – without any changes to the default driver initializer, the browser will open as a small window. R.Price = i.FindElement(By.CssSelector("div.caption > h4.pull-right.price")).Text R.Product_Name = i.FindElement(By.CssSelector("h4 > a")).GetAttribute("title") Var topItems = driver.FindElements(By.CssSelector("div.thumbnail")) Using (IWebDriver driver = new ChromeDriver()) Then we find the name and the price of the items by finding their respective elements (name CSS selector – h4 > a, price CSS selector – div.caption > h4.pull-right.price) inside of the parent item element. First, we find all of the items by their CSS selector ( div.thumbnail). To get the price along with the title of the top items, our script needs only minor modifications.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |