from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 创建Chrome浏览器的实例
driver = webdriver.Chrome()
# 打开页面
driver.get("https://www.baidu.com")
# 显式等待元素加载
wait = WebDriverWait(driver, 10)
form = wait.until(EC.presence_of_element_located((By.ID, "form")))
s_kw_wrap = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "bg")))
input_box = wait.until(EC.presence_of_element_located((By.ID, "kw")))
submit_button = wait.until(EC.presence_of_element_located((By.ID, "su")))
# 获取用户输入的搜索内容
search_query = input("请输入搜索内容: ")
# 对定位到的元素执行操作
input_box.send_keys(search_query)
submit_button.submit()
# 继续其他操作...
# 无限循环,保持浏览器窗口打开
while True:
pass
# 注意:手动终止循环以退出脚本
# 关闭浏览器(在需要的时候关闭)
# driver.quit()
网友评论