前言:
上一篇文章Python|爬虫和测试|selenium框架的安装和初步使用(一)_晚风_END的博客-CSDN博客 大概介绍了一下selenium的安装和初步使用,主要是打开某个网站的主页,基本是最基础的东西,那么,这篇文章里就写一点更加深入的东西吧。
主要是介绍比如,selenium网页刷新,模拟登录csdn,元素定位等等内容
一,
无头浏览器
什么是无头浏览器呢?其实就是selenium后台启动一个浏览器,该浏览器看不到,以节约测试用机的资源。
options.add_argument("headless")主要是这个,其次是截图,截图保存在了d盘,否则不知道是否确实运行了
#codding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = Options()
options.binary_location = "C:\\Users\\Administrator\\Desktop\\chrome\\Chrome-bin\\chrome.exe"
options.add_experimental_option("detach", True)
options.add_argument("headless")
path=Service('f:\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=path)
# 截图预览
driver.get("https://www.csdn.net")
driver.get_screenshot_as_file('d:\\截图.png')
二,
刷新页面
关闭无头,以在前台观察是否确实刷新,增加刷新代码,主要是driver.refresh()方法
#codding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = Options()
options.binary_location = "C:\\Users\\Administrator\\Desktop\\chrome\\Chrome-bin\\chrome.exe"
options.add_experimental_option("detach", True)
#options.add_argument("headless")
path=Service('f:\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=path)
# 截图预览
driver.get("https://www.csdn.net")
time.sleep(2)
driver.get_screenshot_as_file('d:\\截图.png')
try:
# 刷新页面
driver.refresh()
print('刷新页面')
except Exception as e:
print('刷新失败')
执行完毕后,cmd的截图:
表明确实刷新了页面
三,
csdn首页输入框输入指定字符
#codding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = Options()
options.binary_location = "C:\\Users\\Administrator\\Desktop\\chrome\\Chrome-bin\\chrome.exe"
options.add_experimental_option("detach", True)
#options.add_argument("headless")
path=Service('f:\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=path)
# 截图预览
driver.get("https://www.csdn.net")
time.sleep(2)
driver.get_screenshot_as_file('d:\\截图.png')
try:
# 刷新页面
driver.refresh()
print('刷新页面')
except Exception as e:
print('刷新失败')
print(driver.page_source)
driver.find_element(By.XPATH,'//*[@id="toolbar-search-input"]').send_keys('fuck')
运行结果如下:
四,
点击搜索
#codding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = Options()
options.binary_location = "C:\\Users\\Administrator\\Desktop\\chrome\\Chrome-bin\\chrome.exe"
options.add_experimental_option("detach", True)
#options.add_argument("headless")
path=Service('f:\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=path)
# 截图预览
driver.get("https://www.csdn.net")
time.sleep(2)
driver.get_screenshot_as_file('d:\\截图.png')
try:
# 刷新页面
driver.refresh()
print('刷新页面')
except Exception as e:
print('刷新失败')
print(driver.page_source)
driver.find_element(By.XPATH,'//*[@id="toolbar-search-input"]').send_keys('fuck')
driver.find_element(By.XPATH,'//*[@id="toolbar-search-button"]').click()
网页源代码内相关内容如下:
运行结果如下:
说我没有登录,OK,这就登录一下
五,
selenium登录csdn
#codding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = Options()
options.binary_location = "C:\\Users\\Administrator\\Desktop\\chrome\\Chrome-bin\\chrome.exe"
options.add_experimental_option("detach", True)
#options.add_argument("headless")
path=Service('f:\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=path)
# 截图预览
driver.get("https://www.csdn.net")
time.sleep(2)
driver.get_screenshot_as_file('d:\\截图.png')
try:
# 刷新页面
driver.refresh()
print('刷新页面')
except Exception as e:
print('刷新失败')
print(driver.page_source)
driver.find_element(By.XPATH,'//*[@id="toolbar-search-input"]').send_keys('fuck')
#driver.find_element(By.XPATH,'//*[@id="toolbar-search-button"]').click()
login=driver.find_element(By.XPATH, "//*[@class='toolbar-btn-loginfun']")
#login=driver.find_element_by_class_name('toolbar-btn-loginfun')
login.click()
网页源代码相关内容如下:
运行效果如下:
未完待续!!!!