1. 方法 set_window_size(x,y)
比如,改变窗口宽度:
size = driver.get_window_size()
driver.set_window_size(1100,size['height'])
1 from selenium import webdriver 2 3 driver = webdriver.Chrome(r"D:webdriverchromedriver_73.0.3683.68.exe") 4 driver.implicitly_wait(1) 5 driver.get("https://www.baidu.com/") 6 7 #获取当前窗口大小 8 size1 = driver.get_window_size() 9 print(size1) 10 11 #宽度变成1500,高度不变 12 driver.set_window_size(1500,size1['height']) 13 14 #获取改变后窗口大小 15 size2 = driver.get_window_size() 16 print(size2) 17 18 input('......') 19 driver.quit()
结果:
{'width': 945, 'height': 1020}-------------获取窗口尺寸的结果是字典形式的。
{'width': 1500, 'height': 1020}
2. 方法driver.maximize_window()
将窗口最大化
3. 方法driver.execute_script('')
需要滚动到元素的方案 ,可以让浏览器执行javascript脚本
1 from selenium import webdriver 2 3 driver = webdriver.Chrome(r"D:webdriverchromedriver_73.0.3683.68.exe") 4 driver.implicitly_wait(1) 5 driver.get("https://www.cnblogs.com/") 6 7 driver.execute_script('window.scrollBy(0,300)') 8 9 driver.find_element_by_id("cnnic_img").click() 10 11 input("...") 12 driver.quit()