如何使用 selenium 和 GeckoDriver 显示搜索结果?
How to display search results using selenium and GeckoDriver?
我正在尝试使用无头 WebDriver 和 Selenium 打印 DuckDuckgo 的搜索结果。但是,无论我搜索什么 ID 或 class 名称,也无论我等待它加载多长时间,我都找不到引用搜索结果的 DOM 元素。
代码如下:
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 []
我开始怀疑在 DuckDuckGo 中有一些技巧可以避免网页抓取。有人知道吗?
我已经改为使用 cssSelector 然后它 works.I 使用 java,而不是 python。
List<WebElement> elements = driver.findElements(
By.cssSelector(".result.results_links_deep.highlight_d.result--url-above-snippet"));
System.out.println(elements.size());
//10
我正在尝试使用无头 WebDriver 和 Selenium 打印 DuckDuckgo 的搜索结果。但是,无论我搜索什么 ID 或 class 名称,也无论我等待它加载多长时间,我都找不到引用搜索结果的 DOM 元素。
代码如下:
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 []
我开始怀疑在 DuckDuckGo 中有一些技巧可以避免网页抓取。有人知道吗?
我已经改为使用 cssSelector 然后它 works.I 使用 java,而不是 python。
List<WebElement> elements = driver.findElements(
By.cssSelector(".result.results_links_deep.highlight_d.result--url-above-snippet"));
System.out.println(elements.size());
//10