Selenium中提供了不少的方法来操作浏览器
使用driver.get(‘网址’)的方式来打开浏览器
from selenium import webdriver
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
opt = ChromeOptions() # 创建Chrome参数对象
opt.headless = True # 把Chrome设置成可视化无界面模式,windows/Linux 皆可
driver = webdriver.Chrome()# Chrome浏览器
driver.get("网址")
driver.back()#后退
driver.forward()#前进
driver.refresh()
浏览器切换标签的方法就是,
driver.current_window_handle
用selenium点击新打开了一个网页,需要告知selenium目前这个窗口是新的窗口,那么它才能早新的页面进行操作不然的话就无法操作。
from selenium import webdriver
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import wait, expected_conditions
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://so.csdn.net/so/search?spm=1000.2115.3001.7499&q=%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AF%BE%E7%A8%8B%E8%AE%BE%E8%AE%A1&t=&u=")
driver.refresh()
driver.implicitly_wait(5)
driver.find_element(By.XPATH,'//*[@id="search"]').click()
driver.switch_to.window(driver.window_handles[-1])#表示新的窗口
f1=driver.find_element(By.CSS_SELECTOR,'div.item-hd em').text
print(f1)
如果只是在页面进行标签操作的话可以用tab切换到新的标签页而不用切换窗口
# 打开新标签页并切换到新标签页
driver.switch_to.new_window(‘tab’)
操作完毕退出页面是
driver.close()
操作完毕退出浏览器是
driver.quit()
driver.set_window_size(1920,1080)
x = driver.get_window_position().get(‘x’)
y = driver.get_window_position().get(‘y’)
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://so.csdn.net/so/search?spm=1000.2115.3001.7499&q=%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AF%BE%E7%A8%8B%E8%AE%BE%E8%AE%A1&t=&u=")
# 分别获取每个尺寸
x = driver.get_window_position().get('x')
y = driver.get_window_position().get('y')
print(x,y)
driver.maximize_window()
driver.minimize_window()
opt = ChromeOptions() # 创建Chrome参数对象
opt.headless = True # 把Chrome设置成可视化无界面模式,windows/Linux 皆可
driver = Chrome(options=opt) # 创建Chrome无界面对象
from selenium import webdriver
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import wait, expected_conditions
from selenium import webdriver
opt = ChromeOptions() # 创建Chrome参数对象
opt.headless = True # 把Chrome设置成可视化无界面模式,windows/Linux 皆可
driver = Chrome(options=opt) # 创建Chrome无界面对象
driver.get("https://so.csdn.net/so/search?spm=1000.2115.3001.7499&q=%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AF%BE%E7%A8%8B%E8%AE%BE%E8%AE%A1&t=&u=")
driver.refresh()
driver.implicitly_wait(5)
fx=driver.window_handles
print(fx)
driver.find_element(By.XPATH,'//*[@id="search"]').click()
driver.switch_to.window(driver.window_handles[-1])
f1=driver.find_element(By.CSS_SELECTOR,'div.item-hd em').text
print(f1)
driver.fullscreen_window()
driver.save_screenshot(‘./1.png’)
from selenium import webdriver
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import wait, expected_conditions
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://so.csdn.net/so/search?spm=1000.2115.3001.7499&q=%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AF%BE%E7%A8%8B%E8%AE%BE%E8%AE%A1&t=&u=")
driver.refresh()
driver.implicitly_wait(5)
driver.find_element(By.XPATH,'//*[@id="search"]').click()
driver.switch_to.window(driver.window_handles[-1])
f1=driver.find_element(By.CSS_SELECTOR,'div.item-hd em').text
driver.save_screenshot('./1.png')
print(f1)
元素截图只是针对这个元素进行截图,但是这种截图只能是一个大概的范围精准度就不太太好。
f1.screenshot(‘./2.png’)
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://so.csdn.net/so/search?spm=1001.2101.3001.4498&q=XPATH%E5%AE%9A%E4%BD%8D&t=&u=&s=hot")
f1=driver.find_element(By.CSS_SELECTOR,'div.search-box')
f1.screenshot('./2.png')
driver.quit()