Python Selenium3 简单操作进行百度搜索

当前环境:Win10 + Python3.7 + selenium==3.141.0,urllib3==1.26.2

python 复制代码
from selenium import webdriver
import time

if __name__ == '__main__':
    # Chrome 路径
    CHROME_PATH = r'C:\Program Files (x86)\65.0.3312.0\chrome-win32\chrome.exe'
    # ChromeDriver 路径
    CHROMEDRIVER_PATH = r'C:\Program Files (x86)\65.0.3312.0\chromedriver_win32\chromedriver.exe'

    options = webdriver.ChromeOptions()
    # 取消 Chrome 正受到自动测试软件的控制
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    # 取消 请停用以开发者模式运行的扩展程序
    options.add_experimental_option("useAutomationExtension", False)
    # 手动指定使用的浏览器位置
    options.binary_location = CHROME_PATH
    driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options)

    url = 'https://www.baidu.com'
    driver.get(url)
    print(driver.title)

    # 输入框
    element = driver.find_element_by_id('kw')
    element.clear()
    element.send_keys('你好')

    # 百度一下
    element = driver.find_element_by_id('su')
    element.click()

    time.sleep(3)
    print(driver.title)

    # 关闭当前显示的窗口
    driver.close()
    # 退出浏览器
    driver.quit()

    '''
    运行结果:
    百度一下,你就知道
    你好_百度搜索
    '''

其他知识:

复制代码
'''
# 检测是否有未结束 chromedriver 进程 将其关闭
import os
os.system('chcp 65001')
os.system('taskkill /F /IM chromedriver.exe')
'''
复制代码
'''
selenium3  定位元素方法
driver.find_element_by_class_name("className")
driver.find_element_by_css_selector(".className")
driver.find_element_by_id("elementId")
driver.find_element_by_link_text("linkText")
driver.find_element_by_name("elementName")
driver.find_element_by_partial_link_text("partialText")
driver.find_element_by_tag_name("elementTagName")
driver.find_element_by_xpath("xpath")
'''

'''
selenium3 定位多个元素方法
driver.find_elements_by_class_name("className")
driver.find_elements_by_css_selector(".className")
driver.find_elements_by_id("elementId")
driver.find_elements_by_link_text("linkText")
driver.find_elements_by_name("elementName")
driver.find_elements_by_partial_link_text("partialText")
driver.find_elements_by_tag_name("elementTagName")
driver.find_elements_by_xpath("xpath")
'''
复制代码
'''
selenium3 executable_path 写法
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options)
'''
复制代码
'''
driver.back(): 返回到跳转前的页面。
driver.close(): 关闭当前显示的窗口。如果打开多个窗口,也只会关闭当前的窗口,其他窗口正常显示。
driver.quit(): 退出浏览器, 不管打开的是几个窗口,全部退出。
'''

参考:

https://www.cnblogs.com/kxtomato/p/16403798.html

driver.back() 、driver.close() 、driver.quit()三者的区别-CSDN博客

相关推荐
Superstarimage3 分钟前
使用conda创建python虚拟环境,并自定义路径
windows·python·conda
菜鸡码农,喵。4 分钟前
已经装了pygame但pycharm显示没有该模块/软件包无法加载出来下载pygame
python·pycharm·pygame
小羊Linux客栈10 分钟前
自动化:批量文件重命名
运维·人工智能·python·自动化·游戏程序
shykevin3 小时前
python开发Streamable HTTP MCP应用
开发语言·网络·python·网络协议·http
我不是程序猿儿3 小时前
【C#】 lock 关键字
java·开发语言·c#
漫路在线4 小时前
JS逆向-某易云音乐下载器
开发语言·javascript·爬虫·python
小辉懂编程4 小时前
C语言:51单片机实现数码管依次循环显示【1~F】课堂练习
c语言·开发语言·51单片机
醍醐三叶5 小时前
C++类与对象--2 对象的初始化和清理
开发语言·c++
Magnum Lehar6 小时前
3d游戏引擎EngineTest的系统实现3
java·开发语言·游戏引擎
Mcworld8576 小时前
java集合
java·开发语言·windows