# 导入依赖库 from selenium import webdriver from selenium.webdriver.common.keys import Keys # 设置浏览器对象 driver = webdriver.Chrome() # 打开需要登录的网站 driver.get("https://example.com/login") # 输入用户名和密码 username = driver.find_element_by_id("username") username.send_keys("your_username") password = driver.find_element_by_id("password") password.send_keys("your_password") # 登录 submit_button = driver.find_element_by_xpath("//button[@type='submit']") submit_button.click() # 在搜索框中输入关键词并搜索文章 search_input = driver.find_element_by_id("search-input") search_text = "文章关键词" search_input.send_keys(search_text) search_input.send_keys(Keys.RETURN) # 获取搜索结果列表中每篇文章的标题 result_list = driver.find_elements_by_class_name("result") for result in result_list: title = result.find_element_by_class_name("title").text print(title) # 退出浏览器 driver.quit()