一,代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
def change_url(url):
# 就在当前标签页打开网页
driver.get(url)
print(driver.title)
# 获取所有窗口句柄并切换到最后一个
handles = driver.window_handles
driver.switch_to.window(handles[-1])
def open_new_url(url):
# 就在当前标签页打开网页
driver.execute_script("window.open('"+url+"', '_blank');")
# 获取所有窗口句柄并切换到最后一个
handles = driver.window_handles
driver.switch_to.window(handles[-1])
if __name__ == '__main__':
# 指定驱动路径
driver_path = '/opt/soft/chromedriver-linux64/chromedriver'
# 创建Service对象并传入ChromeOptions
service = Service(driver_path)
# 得到options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(options=chrome_options,service=service)
# 通过JavaScript在新窗口打开URL
URL = 'https://test.xxx.cn/websitelogin.html'
open_new_url(URL)
time.sleep(3)
# 打开滑动时的浮动框
"""基础鼠标悬停操作"""
# 定位需要悬停的元素
code_btn = driver.find_element(By.ID, "id-code-btn")
# 创建ActionChains实例
actions = ActionChains(driver)
# 构建悬停操作链
actions.move_to_element(code_btn) # 移动鼠标到元素
# 执行操作
actions.perform()
print("鼠标悬停操作执行完成")