影刀RPA结合Selenium自动化实现数据流互通
评论
收藏

影刀RPA结合Selenium自动化实现数据流互通

经验分享
飞
飞絮
2024-08-02 11:46·浏览量:2341
飞
飞絮
发布于 2024-08-02 11:462341浏览

Selenium的必要性:影刀的指令部分参数不完整,例如,获取一个懒加载网页的总像素数量。该值对于精准计算元素大小起到关键性作用。

安装selenium库以及webdriver----------->webdriver与chrome的版本号要一致。例如chrome的版本是127,对应的chrome webdriver的版本也是127-------webdriver的下载镜像 https://github.com/dreamshao/chromedriver

如何实现影刀与Selenium控制同一个网页对象?

使用浏览器session通信功能。启动端口号为9222 的浏览器(已经安装了影刀插件)

例如:该函数实现了

def start_chrome():

   chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

   user_data_dir = r"C:\selenium\ChromeProfile"

   command = f'"{chrome_path}" --remote-debugging-port=9222 --user-data-dir="{user_data_dir}"'

   

   try:

       # 使用subprocess.Popen启动Chrome浏览器

       subprocess.Popen(command, shell=True)

       print("Chrome启动成功")

   except Exception as e:

       print(f"启动Chrome时出错: {e}")

--------------------------------------------------------------------------------------------------------------------------------------------------------------

这一步代码是调用selenium的方法。selenium控制端口号为9222 的浏览器,并打开网页 driver.get("https://www.goofish.com/")。

def main(flag):

   start_chrome()

   # ChromeDriver的路径

   driver_path = r"C:\Users\16552\Desktop\chromedriver-win64\chromedriver.exe"

   # 创建Service对象

   service = Service(driver_path)

   # 创建ChromeOptions实例(如果有额外设置)

   options = webdriver.ChromeOptions()

   #options.add_experimental_option('detach', True)

   options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")

   # 实例化Chrome浏览器

   driver = webdriver.Chrome(service=service, options=options)

   

   # 访问Google首页

   driver.get("https://www.goofish.com/")

   # 获取初始页面高度

   last_height = driver.execute_script("return document.body.scrollHeight")

   while True:

       # 模拟鼠标滚轮向下滚动

       driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

       time.sleep(3)  # 等待新内容加载

       

       # 获取新页面高度

       new_height = driver.execute_script("return document.body.scrollHeight")

       

       # 如果页面高度没有变化,则退出循环

       if new_height == last_height:

           break

       

       last_height = new_height

   

   # 打印最终页面高度

   print(f"总页面高度: {last_height}")

--------------------------------------------------------------------------------------------------------------------------------------------------------------

调用模块python代码到主流程中,影刀接管selenium处理的网页对象。数据完全通用



   

收藏6
全部评论(1)
最新
发布评论
评论