selenium自動化測試工具python筆試面試專案實戰6 javascript操作
說明
-
參考書籍下載:
2018最佳人工智慧資料採集(爬蟲)工具書下載
Learning Selenium Testing Tools with Python-2014.pdf
Selenium自動化測試 基於 Python 語言 - 2018.pdf
selenium 上機實操: 越過瀏覽器查詢更多記錄
-
開啟: ping.com/places/default/search" target="_blank" rel="nofollow,noindex">http://example.webscraping.com/places/default/search
-
在"Name"框輸入".",在"Page size"中輸入"300"

圖片.png
參考答案
#!/usr/bin/python3 # -*- coding: utf-8 -*- # 討論釘釘免費群21745728 qq群144081101 567351477 # CreateDate: 2018-10-20 from selenium import webdriver driver = webdriver.Firefox() driver.implicitly_wait(30) driver.get('http://example.webscraping.com/places/default/search') driver.find_element_by_id('search_term').send_keys('.') js = "document.getElementById('page_size').options[1].text = '300';" driver.execute_script(js) driver.find_element_by_id('search').click() links = driver.find_elements_by_css_selector('#results a') countries = [link.text for link in links] print(len(countries)) print(countries) driver.close()
本例參考書籍: 用Python寫網路爬蟲.pdf
selenium 上機實操: 下拉重新整理框中所有內容(javascript實現)
- 開啟: http://www.webscrapingfordatascience.com/complexjavascript/
- 抓取框中所有內容。該框按住滑鼠中鍵下滾,會有重新整理內容,直至完全載入

圖片.png
參考答案
#!/usr/bin/python3 # -*- coding: utf-8 -*- # 討論釘釘免費群21745728 qq群144081101 567351477 # CreateDate: 2018-10-18 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException class at_least_n_elements_found(object): def __init__(self, locator, n): self.locator = locator self.n = n def __call__(self, driver): elements = driver.find_elements(*self.locator) if len(elements) >= self.n: return elements else: return False url = 'http://www.webscrapingfordatascience.com/complexjavascript/' driver = webdriver.Chrome() driver.get(url) # Use an implicit wait for cases where we don't use an explicit one driver.implicitly_wait(10) div_element = driver.find_element_by_class_name('infinite-scroll') quotes_locator = (By.CSS_SELECTOR, ".quote:not(.decode)") nr_quotes = 0 while True: # Scroll down to the bottom driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', div_element) # Try to fetch at least nr_quotes+1 quotes try: all_quotes = WebDriverWait(driver, 3).until( at_least_n_elements_found(quotes_locator, nr_quotes + 1)) except TimeoutException as ex: # No new quotes found within 3 seconds, assume this is all there is print("... done!") break # Otherwise, update the quote counter nr_quotes = len(all_quotes) print("... now seeing", nr_quotes, "quotes") # all_quotes will contain all the quote elements print(len(all_quotes), 'quotes found\n') for quote in all_quotes: print(quote.text) input('Press ENTER to close the automated browser') driver.quit()
- 面試問答
1.execute_script()有什麼用途?