記錄一些常用命令(待更新)。
from selenium import webdriver #等待 from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import TimeoutException #鍵盤 from selenium.webdriver.common.keys import Keys #select元素 from selenium.webdriver.support.ui import Select ##配置並開啟火狐 firefox_profile = webdriver.FirefoxProfile() #語言設置為zh-CN,en-US firefox_profile.set_preference('intl.accept_languages','zh-CN') #UA設置為iphone6 plus firefox_profile.set_preference("general.useragent.override", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1") #2關閉瀏覽器圖片 firefox_profile.set_preference('permissions.default.image', 2) driver = webdriver.Firefox(firefox_profile=firefox_profile) #窗口大小 driver.set_window_size(414,736) #窗口最大化,可解決一些元素因窗口太小為顯示而無法點擊的情況 driver.maximize_window() #頁面超時 driver.set_page_load_timeout(50) ##給火狐設置 http 或 socks 代理 firefox_profile = webdriver.FirefoxProfile() proxy_ip = "127.0.0.1" proxy_port = 18409 firefox_profile.set_preference("network.proxy.type", 1) #http代理需要設置下面四行 firefox_profile.set_preference("network.proxy.http", str(proxy_ip)) firefox_profile.set_preference("network.proxy.http_port", int(proxy_port)) firefox_profile.set_preference("network.proxy.ssl", str(proxy_ip)) firefox_profile.set_preference("network.proxy.ssl_port", int(proxy_port)) #socks 代理只需設置下面兩行 firefox_profile.set_preference("network.proxy.socks", str(proxy_ip)) firefox_profile.set_preference("network.proxy.socks_port", int(proxy_port)) #可選 firefox_profile.set_preference("network.http.use-cache", False) #可選 firefox_profile.update_preferences() driver = webdriver.Firefox(firefox_profile=firefox_profile) #在無界面模式下運行 headless from selenium.webdriver.firefox.options import Options as FirefoxOptions options = FirefoxOptions() options.add_argument("--headless") driver = webdriver.Firefox(firefox_profile=firefox_profile,options=options) ##driver的完整初始化參數 webdriver.Firefox(firefox_profile=None, firefox_binary=None, capabilities=None, proxy=None, executable_path=DEFAULT_EXECUTABLE_PATH, options=None, service_log_path=DEFAULT_SERVICE_LOG_PATH, service_args=None, service=None, desired_capabilities=None, log_path=DEFAULT_LOG_PATH, keep_alive=True) #https://github.com/SeleniumHQ/selenium/blob/trunk/py/selenium/webdriver/firefox/webdriver.py#L44
##頁面操作 #打開頁面 try: driver.get("https://reportaproblem.apple.com") except: try: #長時間沒加載完頁面時,可能是卡在了某個資源,按下Esc停止等待 driver.find_element_by_xpath('//body').send_keys(Keys.ESCAPE) except: return 'fail' #有時鏈接會打開新窗口,切換到新窗口 driver.switch_to_window(driver.window_handles[-1]) #當前頁面鏈接 driver.current_url #當前頁面標題 driver.title #等待元素出現 try: WebDriverWait(driver, 60).until(lambda the_driver: the_driver.find_element_by_xpath("//iframe[@id='aid-auth-widget-iFrame']").is_displayed()) except: pass ##定位元素 #frame driver.find_element_by_xpath("//iframe[@id='aid-auth-widget-iFrame']") #ID driver.find_element_by_id("appleId") #根據屬性定位元素 captchaInput = driver.find_element_by_xpath("//input[@placeholder='键入图中的字符']") #獲取輸入框中的值 inputValue = captchaInput.get_attribute('value') #向輸入框中寫入字符 captchaInput.send_keys("你好") #清空輸入框 captchaInput.clear() #按下Tab鍵 driver.find_element_by_xpath('//body').send_keys(Keys.TAB) capDivImg=driver.find_element_by_xpath("//div[@class='form-cell']//img") #獲取圖片的鏈接 imgSrc=capDivImg.get_attribute('src') #點擊包含“繼續”的按鈕 driver.find_element_by_xpath("//button[contains(text(),'继续')]").click() #根據CSS定位button driver.find_element_by_xpath("//button[@class='button button-primary last nav-action']") #點擊值為“步行”的選項 driver.find_element_by_xpath("//option[text()='步行']").click() #定位一組元素 labels = driver.find_elements_by_css_selector("label.labelClass") answerInputs = driver.find_elements_by_xpath("//input[@placeholder='答案']") questions = [] for label in labels: questions.append( label.get_attribute('innerHTML') ) #等待包含特定字符的元素 WebDriverWait(driver, 3).until(lambda the_driver: the_driver .find_element_by_xpath("//p[@class='subtitle content-item tk-label'][contains(text(),'已超时')]") .is_displayed()) #獲取select元素 typeS = Select(driver.find_element_by_id("searchDropdownBox")) #根據值來選中select元素 typeS.select_by_visible_text('行山') #獲取元素顯示的文字 driver.find_element_by_xpath("//tr[@class='updateblock']//td[@class='timer']").get_attribute("innerText") driver.find_element_by_xpath("//tr[@class='updateblock']//td[@class='timer']").get_attribute("innerHTML")
命令行啟動瀏覽器
firefox -width 1440 -height 960 google-chrome --window-size="1440,960"
本文更新於 2024/09/01。