利用XPath截图

from selenium import webdriver
import time
from PIL import Image

import os
from pathlib import Path
my_file = Path('k:/zhusc/2.png')
if my_file.exists():
    os.remove(my_file)

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def wait_for_you(myDynamicElement):
    try:
    #查看10秒内是否出现该元素,如果元素10秒内出现就返回,否则抛出异常。
        element=WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.XPATH,myDynamicElement))
        )
    finally:
        driver.find_element_by_xpath(myDynamicElement).click() 

url="https://vip.gxrc.com/Public/Phone/6C73242B-DE0C-490B-A715-3DF7BCED7825"#广西人才网
driver=webdriver.Chrome('k:/zhusc/chromedriver.exe')
driver.get(url)
driver.maximize_window() #窗口最大化
myDynamicElement='/html/body/img'
try:
    #查看10秒内是否出现该元素,如果元素10秒内出现就返回,否则抛出异常。
        element=WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.XPATH,myDynamicElement))
        )
except:
    pass
driver.get_screenshot_as_file('k:/zhusc/2.png')
e = driver.find_element_by_xpath('/html/body/img')#联系电话
location = e.location
size = e.size
left = e.location['x']
top = e.location['y']
right = e.location['x'] + e.size['width']
bottom = e.location['y'] + e.size['height']

im = Image.open('k:/zhusc/2.png') 
im = im.crop((left, top, right, bottom))
im.save('k:/zhusc/2.png')

你可能感兴趣的:(selenium,xpath,python)