点击第一页的视频标题

elements = driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']") for i in range(3): try: # 在这里执行你想要的操作,例如获取元素文本、点击元素等 elements = driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']") element = elements[i] print(element.text) # 打印元素文本 # 生成随机的点击延迟时间 delay = random.uniform(3, 5) time.sleep(delay) element.click() # 点击元素 time.sleep(10)

    # 获取当前窗口句柄
    current_window = driver.current_window_handle
    # 切换到第三个窗口
    window_handle = driver.window_handles[2]
    driver.switch_to.window(window_handle)
    # 获取当前页面的URL
    current_url = driver.current_url
    #视频点赞
    video_like_button = driver.find_element_by_xpath("//*[@id ='arc_toolbar_report']/div[1]/div[1]")
    video_like_button.click()
    time.sleep(2)
    #视频收藏
    video_collection_button =  driver.find_element_by_xpath("//*[@id='arc_toolbar_report']/div[1]/div[3]")
    video_collection_button.click()
    time.sleep(2)
    video_collection1_button = driver.find_element_by_xpath("//*[@id='arc_toolbar_report']/div[3]/div/div/div[2]/div/ul/li/label")
    video_collection1_button.click()
    time.sleep(2)
    video_collection2_button = driver.find_element_by_xpath("//*[@id ='arc_toolbar_report']/div[3]/div/div/div[3]/button")
    video_collection2_button.click()
    time.sleep(2)
    # 编辑评论
    comment_input = driver.find_element_by_xpath("//*[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[2]/textarea")
    comment_input.click()
    comment_input.send_keys(comment)
    time.sleep(1)
    # 点击发送评论
    comment_button = driver.find_element_by_xpath("//*[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[3]")
    comment_button.click()
    time.sleep(5)
    #点赞评论
    like_button = driver.find_element_by_xpath("//*[@id ='comment']/div/div/div/div[2]/div[2]/div[1]/div[2]/div[2]/div[3]/div/span[2]/i")
    like_button.click()
    time.sleep(5)
    # 关闭当前窗口,回到搜索结果页面
    driver.close()
    driver.switch_to.window(driver.window_handles[1])
except Exception as e:
    print(str(e))

点击下一页按钮

next_page_button = driver.find_element_by_xpath("//*[@id='i_cecream']/div/div[2]/div[2]/div/div/div/div[3]/div/div/button[2]") next_page_button.click() time.sleep(5)

点击第二页的视频标题

elements = driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']") for i in range(3): try: # 在这里执行你想要的操作,例如获取元素文本、点击元素等 elements = driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']") element = elements[i] print(element.text) # 打印元素文本 # 生成随机的点击延迟时间 delay = random.uniform(3, 5) time.sleep(delay) element.click() # 点击元素 time.sleep(10)

    # 获取当前窗口句柄
    current_window = driver.current_window_handle
    # 切换到第三个窗口
    window_handle = driver.window_handles[2]
    driver.switch_to.window(window_handle)
    # 获取当前页面的URL
    current_url = driver.current_url
    #视频点赞
    video_like_button = driver.find_element_by_xpath("//*[@id ='arc_toolbar_report']/div[1]/div[1]")
    video_like_button.click()
    time.sleep(2)
    #视频收藏
    video_collection_button =  driver.find_element_by_xpath("//*[@id='arc_toolbar_report']/div[1]/div[3]")
    video_collection_button.click()
    time.sleep(2)
    video_collection1_button = driver.find_element_by_xpath("//*[@id='arc_toolbar_report']/div[3]/div/div/div[2]/div/ul/li/label")
    video_collection1_button.click()
    time.sleep(2)
    video_collection2_button = driver.find_element_by_xpath("//*[@id ='arc_toolbar_report']/div[3]/div/div/div[3]/button")
    video_collection2_button.click()
    time.sleep(2)
    # 编辑评论
    comment_input = driver.find_element_by_xpath("//*[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[2]/textarea")
    comment_input.click()
    comment_input.send_keys(comment)
    time.sleep(1)
    # 点击发送评论
    comment_button = driver.find_element_by_xpath("//*[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[3]")
    comment_button.click()
    time.sleep(5)
    #点赞评论
    like_button = driver.find_element_by_xpath("//*[@id ='comment']/div/div/div/div[2]/div[2]/div[1]/div[2]/div[2]/div[3]/div/span[2]/i")
    like_button.click()
    time.sleep(5)
    # 关闭当前窗口,回到搜索结果页面
    driver.close()
    driver.switch_to.window(driver.window_handles[1])
except Exception as e:
    print(str(e))

点击下一页按钮

next_page_button = driver.find_element_by_xpath("//*[@id='i_cecream']/div/div[2]/div[2]/div/div/div/div[3]/div/div/button[3]") next_page_button.click() time.sleep(5)

点击第三页的视频标题

elements = driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']") for i in range(3): try: # 在这里执行你想要的操作,例如获取元素文本、点击元素等 elements = driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']") element = elements[i] print(element.text) # 打印元素文本 # 生成随机的点击延迟时间 delay = random.uniform(3, 5) time.sleep(delay) element.click() # 点击元素 time.sleep(10)

    # 获取当前窗口句柄
    current_window = driver.current_window_handle
    # 切换到第三个窗口
    window_handle = driver.window_handles[2]
    driver.switch_to.window(window_handle)
    # 获取当前页面的URL
    current_url = driver.current_url
    #视频点赞
    video_like_button = driver.find_element_by_xpath("//*[@id ='arc_toolbar_report']/div[1]/div[1]")
    video_like_button.click()
    time.sleep(2)
    #视频收藏
    video_collection_button =  driver.find_element_by_xpath("//*[@id='arc_toolbar_report']/div[1]/div[3]")
    video_collection_button.click()
    time.sleep(2)
    video_collection1_button = driver.find_element_by_xpath("//*[@id='arc_toolbar_report']/div[3]/div/div/div[2]/div/ul/li/label")
    video_collection1_button.click()
    time.sleep(2)
    video_collection2_button = driver.find_element_by_xpath("//*[@id ='arc_toolbar_report']/div[3]/div/div/div[3]/button")
    video_collection2_button.click()
    time.sleep(2)
    # 编辑评论
    comment_input = driver.find_element_by_xpath("//*[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[2]/textarea")
    comment_input.click()
    comment_input.send_keys(comment)
    time.sleep(1)
    # 点击发送评论
    comment_button = driver.find_element_by_xpath("//*[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[3]")
    comment_button.click()
    time.sleep(5)
    #点赞评论
    like_button = driver.find_element_by_xpath("//*[@id ='comment']/div/div/div/div[2]/div[2]/div[1]/div[2]/div[2]/div[3]/div/span[2]/i")
    like_button.click()
    time.sleep(5)
    # 关闭当前窗口,回到搜索结果页面
    driver.close()
    driver.switch_to.window(driver.window_handles[1])
except Exception as e:
    print(str(e)
import timeimport randomfrom selenium import webdriverfrom seleniumwebdrivercommonkeys import Keys# 定义评论内容comment = 我爱坤我爱坤 他是我的梦想我的阳光 他的歌声他的舞姿 让我心动让我疯狂我爱坤我爱坤 他是我的偶像我的英雄 他的笑容他的温柔 让我沉醉让我迷恋我爱坤我爱坤 他是我的一切我

原文地址: https://www.cveoy.top/t/topic/hTjk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录