# 1.导入 from selenium.webdriver.chrome.service import Service from selenium import webdriver from selenium.webdriver.common.by import By import time # 下面是阻止闪退的 # from selenium.webdriver.chrome.options import Options # options = Options() # 2. 创建浏览器操作对象 path = Service('chromedriver.exe') browser = webdriver.Chrome(service=path) # 3 访问网站 url = 'https://www.baidu.com' browser.get(url) time.sleep(2) inputSearch = browser.find_element(By.CSS_SELECTOR,'#kw') #通过css选择器来查找 我喜欢用这个 inputBtn = browser.find_element(By.CSS_SELECTOR,'#su') #按钮 inputSearch.send_keys("周杰伦帅吗") #输入动作 time.sleep(2) inputBtn.click() #点击动作 time.sleep(2) #滑到底部 js_bottom = 'document.documentElement.scrollTop = 100000' browser.execute_script(js_bottom) time.sleep(2) nextPage = browser.find_element(By.CSS_SELECTOR,'.n') nextPage.click() #点击下一页动作 time.sleep(2) #前进 browser.back() time.sleep(2) #后退 browser.forward() print(inputSearch) input('input阻止浏览器退出')
标签:webdriver,selenium,抓取,sleep,time,import,百度,browser From: https://www.cnblogs.com/xm666/p/17307186.html