Files
python/python爬虫/selenium窗口之间的切换.py
T
2025-09-03 11:19:03 +08:00

42 lines
1.3 KiB
Python

from selenium.webdriver import Chrome, Keys
from selenium.webdriver.common.by import By
import time
chrom_dirverpath = r'C:\Users\zy187\IdeaProjects\python\venv\Scripts\chromedriver.exe'
web = Chrome(chromedriver_path=chrom_dirverpath)
# web.get('http://lagou.com')
# web.find_element(By.XPATH, '//*[@id="cboxClose"]').click()
# time.sleep(1)
# web.find_element(By.XPATH, '//*[@id="search_input"]').send_keys('python', Keys.ENTER)
# time.sleep(1)
# # 打开新的标签页
# web.find_element(By.XPATH, '').click()
# time.sleep(1)
# # selenium 新窗口默认不切换
# web.switch_to.window(web.window_handles[-1]) # 选最后一个
#
# # 在新窗口中提取内容
# # ...
# # 关掉窗口
# web.close()
# # 视角返回原窗口
# web.switch_to.window(web.window_handles[0])
# 2 91看剧程序中有iframe
# web.get('https://www.91kanju.com/vod-play/54812-1-2.html')
# # 处理iframe 必须先找到iframe。然后视角跳转iframe
# iframe = web.find_element(By.XPATH, '') # iframe地址
# web.switch_to.frame(iframe)
# # 取内容
# tx = web.find_element(By.XPATH, '').text
# # 返回视角
# web.switch_to.default_content() # 切换默认视角,也就是原视角
# 执行js程序
web.execute_script('window.scrollTo(0,document.body.scrollHeight)') # 滚轮向下滚动一屏高度代码
time.sleep(10000)
web.close()