| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- # -*- coding: utf-8 -*-
- # Author : Charley
- # Python : 3.10.8
- # Date : 2025/8/12 16:10
- from selenium import webdriver
- from selenium.webdriver.chrome.options import Options
- from selenium.webdriver.common.by import By
- import time
- def open_baidu_with_options():
- # 配置Chrome选项
- chrome_options = Options()
- chrome_options.add_argument("--headless=False") # 显示浏览器窗口
- chrome_options.add_argument("--start-maximized") # 最大化窗口
- chrome_options.add_argument("--no-sandbox")
- chrome_options.add_argument("--disable-dev-shm-usage")
- # 启动浏览器
- driver = webdriver.Chrome(options=chrome_options)
- try:
- # 打开百度网站
- driver.get("https://www.baidu.com")
- # 等待页面加载
- time.sleep(3)
- # 验证是否成功打开百度
- if "百度" in driver.title:
- print("成功打开百度首页")
- print(f"页面标题: {driver.title}")
- else:
- print("页面加载可能存在问题")
- # 可以在这里进行其他操作,比如搜索
- # search_box = driver.find_element(By.ID, "kw")
- # search_box.send_keys("Selenium")
- # search_button = driver.find_element(By.ID, "su")
- # search_button.click()
- # time.sleep(3)
- except Exception as e:
- print(f"发生错误: {e}")
- finally:
- # 关闭浏览器
- driver.quit()
- if __name__ == "__main__":
- open_baidu_with_options()
|