selenium一段时间不用,有些常用写法就会忘记,在这里把自己遇到的用法做一下记录。
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--headless") # headless
options.add_argument('user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 9_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13E233 MicroMessenger/6.3.15 NetType/WIFI Language/zh_CN"')
options.add_argument('window-size=1366*768')
# options.add_argument('blink-settings=imagesEnabled=false') # disable loading images
options.add_argument('--disable-javascript') # disable javascript
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) # disable images
options.add_experimental_option('excludeSwitches', ['enable-automation']) # disable the notification of "Chrome is being controlled by automated test software
# as mobile device
mobile_emulation = {
"deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
options.add_experimental_option("mobileEmulation", mobile_emulation)
chrome_driver_path = "/the/driver/path"
browser = webdriver.Chrome(executable_path=chrome_driver_path, chrome_options=options)
browser.set_page_load_timeout(60)
browser.get("https://www.baidu.com")
browser.execute_script("window.open('https://www.baidu.com','_blank');") # open a new tab
browser.execute_script("scroll(0,250)") # scroll down 250
elems = browser.find_elements_by_xpath('//*[@id="mainBox"]/main/div[2]/div')
for elem in elems:
url = elem.find_element_by_xpath("./h4/a").get_attribute('href')
browser.close()