Win7 selenium 自动化chrome

Python3.8.10
selenium可以pip安装

chrome 109最后一个win7 版本,chromedriver 同样需要注意版本(存放位置后使用Service设置路径 )

编写时候注意:

find_elements()返回对象列表,find_element()返回对象!

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

service = Service(r"c:\chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("http://baidu.com/#/login")


#CLASS 选择器
driver.find_element(By.CLASS_NAME,"el-icon-arrow-up").click()
swap_c_s=driver.find_elements(By.CLASS_NAME, "el-select-dropdown__item")
time.sleep(3)
swap_c_s[7].click()
swap_c_v=driver.find_elements(By.CLASS_NAME,"el-input__inner")


#设置信息使用send_keys("")
swap_c_v[1].send_keys("admin")
swap_c_v[2].send_keys("admin")
driver.find_element(By.CLASS_NAME,"el-button--primary").click()

#Xpath选择器
driver.find_element(By.XPATH,'//span[contains(text(),"办理系统")]').click()
time.sleep(3)

打开页面必须 time.sleep 等待页面加载【JS动态页面必须注意AJAX回传也需要等待加载】

swap_tr_ones=driver.find_elements(By.XPATH,'(//table[contains(@class,"el-table__body")])[1]//tr')
for i in swap_tr_ones:
    tid=i.find_element(By.XPATH,'.//td[4]').text
    end=i.find_element(By.XPATH,'.//td[8]').text

#选择页面打开后 可以text 获取信息

打开新页面,需要使用driver.switch_to.window(driver.window_handles[1])
切换到之前的需要driver.switch_to.window(driver.window_handles[0]),上面二者后需要重新使用选择器抓取 ,关闭页面必须使用driver.close()

swap_click_ones=driver.find_elements(By.XPATH,'//table[contains(@class,"el-table__body")]//tr/td[4][not(contains(@class,"hidden"))]')
vx=[]
for i in swap_click_ones:
    i.click()
    time.sleep(3)
    driver.switch_to.window(driver.window_handles[1])
    driver.find_element(By.XPATH,'//span[contains(text(),"查看")]').click()
    #切换到新页面  老的页面 driver.switch_to.window(driver.window_handles[0])
    tid=driver.find_element(By.XPATH,'//label[contains(text(),"abc:")]/following-sibling::div/div').text
    phone=driver.find_element(By.XPATH,'//label[contains(text(),"电话")]/following-sibling::div').text
    info=driver.find_element(By.XPATH,'//label[contains(text(),"内容")]/following-sibling::div').text

    one={
        'tid':tid,
        'phone':phone,
        'info':info,
    }
    vx.append(one)
    driver.close()
    driver.switch_to.window(driver.window_handles[0])

图片保存图片

driver.save_screenshot("full_page.png")

元素保存图片

# 定位图片元素
img = driver.find_element(By.XPATH, "//img")
# 全屏截图临时文件
driver.save_screenshot("tmp.png")
full_img = Image.open("tmp.png")

# 获取元素位置尺寸
x = img.location['x']
y = img.location['y']
w = img.size['width']
h = img.size['height']

# 裁剪并保存
crop = full_img.crop((x, y, x + w, y + h))
crop.save("target_img.png")# 定位图片元素
img = driver.find_element(By.XPATH, "//img")
# 全屏截图临时文件
driver.save_screenshot("tmp.png")
full_img = Image.open("tmp.png")

# 获取元素位置尺寸
x = img.location['x']
y = img.location['y']
w = img.size['width']
h = img.size['height']

# 裁剪并保存
crop = full_img.crop((x, y, x + w, y + h))
crop.save("target_img.png")

Download 下载内容

https://www.python.org/downloads/release/python-3810

https://edgedl.me.gvt1.com/edgedl/release2/chrome/acihtkcueyye3ymoj2afvv7ulzxa_109.0.5414.120/109.0.5414.120_chrome_installer.exe

https://registry.npmmirror.com/binary.html?path=chromedriver/109.0.5414.74

此条目发表在None分类目录。将固定链接加入收藏夹。

发表回复