Search code examples
seleniumgeckodriver

How to display search results using selenium and GeckoDriver?


I'm trying to print search results of DuckDuckgo using a headless WebDriver and Selenium. However, I cannot locate the DOM elements referring to the search results no matter what ID or class name I search for and no matter how long I wait for it to load.

Here's the code:

opts = Options()
opts.headless = False
browser = Firefox(options=opts)

browser.get('https://duckduckgo.com')
search = browser.find_element_by_id('search_form_input_homepage')
search.send_keys("testing")
search.submit()

# wait for URL to change with 15 seconds timeout
WebDriverWait(browser, 15).until(EC.url_changes(browser.current_url))
print(browser.current_url)

results = WebDriverWait(browser,10)
  .until(EC.presence_of_element_located((By.ID,"links")))

time.sleep(10)
results = browser.find_elements_by_class_name('result results_links_deep highlight_d result--url-above-snippet') # I tried many other ID's and class names
print(results) # prints []

I'm starting to suspect there is some trickery to avoid web scraping in DuckDuckGo. Does anyone has a clue?


Solution

  • I've changed to use cssSelector then it works.I use java, not python.

    List<WebElement> elements = driver.findElements(
         By.cssSelector(".result.results_links_deep.highlight_d.result--url-above-snippet"));
    System.out.println(elements.size());
    //10