美文网首页
selenium笔记:模拟用户使用搜索引擎

selenium笔记:模拟用户使用搜索引擎

作者: 我的袜子都是洞 | 来源:发表于2018-11-13 17:31 被阅读9次

实现功能:

  • 打开搜索
  • 输入关键字
  • 点击搜索
  • 结束

实现效果:


实现效果动图

参考代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.wait import WebDriverWait

browser = webdriver.Chrome()
try:
   browser.get("https://www.so.com")
   # 定位搜索框
   input = browser.find_element_by_xpath('//input[@name="q"]')
  # 输入关键字
   input.send_keys("南京地铁")
   # 点击回车
   input.send_keys(Keys.ENTER)
   wait = WebDriverWait(browser,10)
   wait.until(EC.presence_of_element_located((By.ID,'snext')))
   # 打印当前url
   print(browser.current_url)
  # 输出cookie 
   print(browser.get_cookies())
   # 输出网页源代码
   print(browser.page_source)
finally:
   browser.close()

相关文章

网友评论

      本文标题:selenium笔记:模拟用户使用搜索引擎

      本文链接:https://www.haomeiwen.com/subject/ymvlfqtx.html