使用编程语言(如Python的Selenium)获取框架的源码

from selenium import webdriver

# 创建一个新的浏览器实例
driver = webdriver.Firefox()  # 或者 webdriver.Chrome()

# 让浏览器加载包含框架的页面
driver.get('http://example.com')

# 定位到特定的iframe
frame = driver.find_element_by_id('frame_id')  # 替换'frame_id'为实际的frame ID

# 切换到该框架
driver.switch_to.frame(frame)

# 获取框架内容的源码
frame_source = driver.page_source

# 打印或处理frame_source
print(frame_source)

# 切换回主页面
driver.switch_to.default_content()
import time
import tkinter as tk
from datetime import datetime
import urllib.parse
import re
import random
import psutil

from selenium.common.exceptions import ElementClickInterceptedException
# import pyperclip
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains



def js_condition(driver):
   """自定义等待条件函数,检查JavaScript返回值"""
   return driver.execute_script("return document.readyState") == "complete"


def open_edge_browser(url):
   global driver,matches
   # driver.implicitly_wait(10)  # 设置全局隐式等待时间为10秒
   # 定义Edge浏览器的用户数据目录
   edge_user_data_dir = r"C:\Users\dell\AppData\Local\Microsoft\Edge Dev\User Data\Default"

   # 设置Edge选项
   edge_options = Options()
   edge_options.use_chromium = True
   # 向Edge浏览器传递启动参数
   edge_options.add_argument(f"--user-data-dir={edge_user_data_dir}")
   # edge_options.add_argument('--disable-extensions')  # 禁用浏览器扩展
   edge_options.add_argument('--disable-gpu')  # 禁用GPU硬件加速
   # edge_options.add_argument('--headless')  # 禁用GPU硬件加速
   driver = webdriver.Edge(options=edge_options)
   driver.implicitly_wait(40)  # 设置全局隐式等待时间为40秒
   driver.maximize_window()

   driver.get(url)
   WebDriverWait(driver, 20).until(js_condition)

   print("网页已加载完成。")
def go_tasks(driver):
   """完成任务1"""
   # 等待并切换到具有唯一高度属性的iframe
   wait = WebDriverWait(driver, 30)
   unique_height = "644px"  # 根据实际情况调整这个值
   frame_locator = (By.XPATH, f"//iframe[starts-with(@id, 'w') and contains(@style, 'height: {unique_height};')]")
   target_frame = wait.until(EC.presence_of_element_located(frame_locator))

   # driver.execute_script("window.scrollTo(0, 0;")
   driver.switch_to.frame(target_frame)


   # 获取框架内容的源码
   frame_source = driver.page_source
   pattern = r'.(cl\d+)\s+a::before'

   # 在源码中查找匹配项
   matches = re.findall(pattern,frame_source, re.IGNORECASE)
   # 打印或处理frame_source
   print("不要点着:",matches)
   if not matches:
      print("没找到匹配项,请检查")
      # break
      return False
   else:
      try:
         # tasks_css_selector 是待办事项的CSS选择器
         tasks_css_selector = f".ctner:not([class*={matches[0]}])"
         tasks = driver.find_elements(By.CSS_SELECTOR, tasks_css_selector)
         print("任务数量:",len(tasks))
         for index,task in enumerate (tasks):
            # 获取 title 的内容
            title = task.find_element(By.CLASS_NAME, 'title').text
            if "Clee Bour" in title:
               print("跳过:",title)
            print(f"task{index+1}:{title}")
            task.click()
            time.sleep(random.randint(20, 30))
         print("任务完成")
         # 退出当前框架,回到顶级窗口
         kill_edge(driver)
         driver.switch_to.default_content()
         driver.switch_to_window(driver.window_handles[0])
         driver.refresh()

      except Exception as e:
         print("没找到广告")


def kill_edge(driver):
   # 获取Edge浏览器的进程
   driver_processes = [p for p in psutil.process_iter(['name']) if p.info['name'] == 'edgedriver.exe']

   # 关闭除第一个进程外的所有进程
   for p in driver_processes[1:]:
      p.terminate()  # 或者使用 p.kill() 来强制结束


def run_and_refresh(driver):

   while True:
      go_tasks(driver)
      time.sleep(random.randint(10, 20))

def main():
   window = tk.Tk()
   window.title("leadsleap")
   window.geometry("500x300")
   url = "https://leadsleap.com/members/"
   # 创建按钮
   button_open = tk.Button(window, text="准备浏览器环境", command=lambda: open_edge_browser(url))
   button_open.pack()
   # 创建按钮
   button_get_answers = tk.Button(window, text="可以的", command=lambda: run_and_refresh(driver))
   button_get_answers.pack()


   window.mainloop()


if __name__ == "__main__":
   main()

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PyAIGCMaster

1毛钱也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值