from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5, 0.1)
driver.maximize_window()
driver.get("https://www.baidu.com")
element = driver.find_element(By.XPATH, '//*[@id="s-usersetting-top"]')
# 鼠标悬浮
ActionChains(driver).move_to_element(element).perform()
sleep(2)
driver.find_element(By.LINK_TEXT, '搜索设置').click()
wait.until(expected_conditions.presence_of_element_located((By.LINK_TEXT, '保存设置')), '未找到元素')
# 遍历元素
elements = driver.find_elements(By.NAME, "SL")
for element in elements[0:2]:
element.click()
sleep(2)
# 截图到指定文件夹
driver.save_screenshot(R'C:\Users\Administrator\Desktop\jt\caa.png')
driver.find_element(By.LINK_TEXT, '保存设置').click()
sleep(1)
# 打印弹窗文本
print(driver.switch_to.alert.text)
# 确认弹窗按钮
driver.switch_to.alert.accept()
# 取消按钮
# driver.switch_to.alert.dismiss()
# 输入内容
# driver.find_element(By.**,**).click()
# driver.switch_to.alert.dismiss()
driver.quit()
python_selenium 截图到指定文件夹
最新推荐文章于 2024-06-27 16:19:33 发布