C# Alternatives to Headless Browsers

I have implemented the following C# code utilizing Selenium and PhantomJS to automate adding a product to my cart and completing the checkout process. However, I often encounter an error indicating that the element with the product ID ‘pdp_selectedSize’ cannot be found. My suspicion is that this happens because the web driver hasn’t fully loaded the page yet. I would like advice on how to continuously check for this element’s presence without relying on predetermined timeouts. Additionally, I am seeking recommendations for quicker alternatives to headless browsers. I specifically chose headless browsers over HTTP requests due to the necessity of certain cookies for the checkout process, which are set via JavaScript on the webpage. Any suggestions would be greatly appreciated, thank you!

For checking element presence without timeouts in Selenium, use WebDriverWait with ExpectedConditions.ElementExists. It keeps checking until it finds the element or a timeout occurs.

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var element = wait.Until(ExpectedConditions.ElementExists(By.Id("pdp_selectedSize")));

For faster alternatives, consider:

  • Playwright: Faster than Selenium, supports headless mode, and offers automatic waiting for elements.
  • PuppeteerSharp: A C# port of Puppeteer, useful for working with JavaScript-heavy sites.

Hope this helps!