selenium——页面操作(截屏)

1.对于鼠标悬停有下拉框组件的页面,不好定位的,就可以用:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

driver=webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('https//www.baidu.com/')

#鼠标悬停至’设置‘链接
link=driver.find_element_by_link_text('设置')
ActionChains(driver).move_to_element(link).perform()

#打开搜索设置
driver.find_element_by_link_text('搜索设置').click()

#保存设置
driver.find_element_by_class_name('prefpanslgo').click()
time.sleep(2)

#接受警告框
driver.switch_to_alert().accept()
driver.quit()

-----------------------------------------------------------------------

2.设置页面大小和执行js代码操作滚动条

from selenium import webdriver
from time import sleep,ctime
from selenium.webdriver.common.by import By
import os

driver=webdriver.Chrome()
driver.get('https://www.baidu.com/')
# driver.set_window_size(600,600)
driver.find_element_by_id('kw').send_keys('selenium')
driver.find_element_by_id('su').click()
sleep(3)

js='window.scrollTo(100,450);'
driver.execute_script(js)
sleep(3)
driver.quit()

-----------------------------------------------------------------------

3.获取当前页面截屏+存储到指定位置

driver.get_screenshot_as_file('D:\PycharmProjects\wangyongha\Selenium学习\截屏.png')

你可能感兴趣的:(selenium——页面操作(截屏))