python selenium 截取页面区域为图片

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
driver.maximize_window()
start_url = "https://antirobot.tianyancha.com/captcha/verify?return_url=https%3A%2F%2Fwww.tianyancha.com%2Fsearch%2FocH-e15-s2%3Fbase%3Dhangzhou%26areaCode%3D330185&rnd="
driver.get(start_url)
time.sleep(1)
# 获取区域块
imgelement = driver.find_element_by_xpath('//*[@id="bgImgie"]')
#图片坐标
locations = imgelement.location
print(locations)
#图片大小
sizes = imgelement.size
print(sizes)
# 构造指数的位置
rangle = (int(locations['x']),int(locations['y']),int(locations['x'] + sizes['width']),int(locations['y'] + sizes['height']))
print(rangle)
save_path = "C:\\Users\\123\\Desktop\\reptile\\test.png"
driver.save_screenshot(save_path )
# 打开截图切割
img = Image.open(save_path )
jpg = img.convert('RGB')
jpg = img.crop(rangle)
path = "C:\\Users\\123\\Desktop\\reptile\\test9.png"
jpg.save(path)
print("图片截取成功!")

猜你喜欢

转载自blog.csdn.net/weixin_41858542/article/details/87971239
今日推荐