NEWStandard createTask and in.php APIs. Change the address and you are connected.View API docs
GATESOLVER

Web scraping

How do I combine Beautiful Soup with Selenium?

Use Selenium to load and render the page, wait until the data you want is present, then pass driver.page_source to BeautifulSoup and do all the extraction there.

How the two fit together

Selenium drives a real browser, so it runs the JavaScript that builds the page. Beautiful Soup cannot run scripts but parses a finished document quickly and offers a friendly search API. The usual split is simple: Selenium navigates, clicks and waits; once the content is on screen you take a snapshot of the HTML with driver.page_source and parse that snapshot with Beautiful Soup.

A working pattern

import bs4
from selenium.webdriver import ChromeOptions, Chrome as ChromeDriver
from selenium.webdriver.common.by import By  # locator kinds
from selenium.webdriver.support.wait import WebDriverWait  # polls until a condition holds
from selenium.webdriver.support import expected_conditions as cond

rental_opts = ChromeOptions()
rental_opts.add_argument("--headless=new")
browser = ChromeDriver(options=rental_opts)
try:
    browser.get("https://listings.example.com/rentals?city=porto")
    WebDriverWait(browser, 15).until(
        cond.presence_of_element_located((By.CSS_SELECTOR, "div.result-row"))
    )
    snapshot = bs4.BeautifulSoup(browser.page_source, "lxml")
finally:
    browser.quit()

for row in snapshot.select("div.result-row"):
    label = row.select_one(".result-title")
    rent = row.select_one(".result-price")
    print(label.get_text(strip=True) if label else "?",
          rent.get_text(strip=True) if rent else "?")

Note that the browser is closed before parsing starts. Parsing a snapshot does not need the browser, so you free its memory sooner and the parsing loop cannot be broken by the page changing underneath it.

Why not just use Selenium for everything?

You can, but every find_element call is a round trip to the browser through the WebDriver protocol. Reading fifty fields from two hundred rows means ten thousand round trips. One page_source call followed by in-memory parsing is far quicker and keeps the code shorter. Selenium element handles can also go stale when the page re-renders; a parsed snapshot never does.

Timing is the part people get wrong

The snapshot contains whatever the DOM held at that instant. Grab it too early and the list is empty or half filled. Wait on a specific signal rather than a fixed sleep:

  • presence of the element that holds your data, as in the example;
  • a count condition, for example at least 20 rows, written as a small lambda passed to until;
  • the disappearance of a loading spinner via invisibility_of_element_located.

The dedicated page on waiting for a page to load in Selenium goes through these conditions in detail.

Handling pages that need interaction

For paginated or expandable content, loop in Selenium: click the next button or expander, wait for the new content, take another snapshot and parse it. Keep the parsing code in its own function that accepts an HTML string. That way you can test it against saved files without launching a browser at all, which makes selector fixes much faster.

When a CAPTCHA shows up in the browser

A rendered page may stop at a reCAPTCHA or Turnstile widget instead of the data. Parsing will not help there. Read the site key from the page, create a task with the GateSolver API, and inject the returned token into the field or callback, then continue. The Selenium integration guide shows the injection step with full code.

Common mistakes

  • Parsing inside an iframe: page_source returns only the current frame. Switch into the frame first if the data lives there.
  • Mixing parsers: use the same parser for saved test files and live snapshots so results match.
  • Forgetting quit(): wrap the session in try/finally so crashed runs do not leave browsers running.

Keeping the browser part small

A browser is the expensive piece of this setup: it uses hundreds of megabytes of memory and seconds of CPU per page. Look for ways to use it less. Sometimes the rendered page fetches its data from a JSON endpoint that you can call directly once you have seen it in the Network tab, which removes Selenium from the steady state entirely. Other times only the first page needs a browser (to get past a check or set cookies), and later pages can be fetched with a plain HTTP client that reuses those cookies, then parsed with the same Beautiful Soup code.

When you do need the browser for every page, reuse one session across many URLs instead of starting Chrome for each, and block heavy resources you do not parse, such as video. Save a few raw snapshots to disk during development. They double as test fixtures for your parsing function and as evidence when a field suddenly comes back empty in production: you can see whether the page changed or your code did.

Common questions

Does page_source include changes made by JavaScript?

Yes. It serializes the current DOM, so content added by scripts up to that moment is included.

Can I parse shadow DOM content with Beautiful Soup?

Not from page_source, which does not include shadow roots. Read those parts through Selenium or with a script call.

Is it faster to use lxml or html.parser here?

lxml is generally faster on large rendered pages, but either works; keep one choice for consistency.

Should I parse each page in the browser loop?

Collect snapshots in the loop and parse them in a separate function, so browser code and parsing code stay independent.

Can I use Beautiful Soup with Playwright instead?

Yes. page.content() returns the rendered HTML, and you parse it the same way.

Keep reading