Skip to content

Instantly share code, notes, and snippets.

@lhsfcboy
Last active September 8, 2024 19:17
Show Gist options
  • Save lhsfcboy/38155a81e063a8015bd6e666a42cb58c to your computer and use it in GitHub Desktop.
Save lhsfcboy/38155a81e063a8015bd6e666a42cb58c to your computer and use it in GitHub Desktop.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
# 设置ChromeDriver路径 注意不要下载成了无头模式的chrome-headless-shell
chrome_driver_path = r"C://AMD//chromedriver.exe"
service = Service(executable_path=chrome_driver_path)
# 启动Chrome浏览器(无头模式)
driver = webdriver.Chrome(service=service)
# 打开某个网址
driver.get("https://www.example.com")
# 让浏览器等待几秒钟,确保页面加载完成
time.sleep(3)
# 查找输入框并输入文字
input_box = driver.find_element(By.NAME, "username")
input_box.send_keys("your_input_text")
# 模拟按下回车键
input_box.send_keys(Keys.RETURN)
# 等待页面响应
time.sleep(3)
# 关闭浏览器
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment