from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#driver = webdriver.Chrome(executable_path='./chromedriver/chromedriver')
from selenium.webdriver.support.wait import WebDriverWait
def open(url1) :
chrome_options = Options()
# 设置浏览器的无头浏览模式 无头无界面 那在linux下就可以运行了
chrome_options.add_argument('--headless')
# 避免 DevToolsActivePort 不存在报错的形式
chrome_options.add_argument('--no-sandbox')
# 官方关闭的一些关键选项,规避一些BUG
chrome_options.add_argument('--disable-gpu')
#使得浏览器执行完程序之后不闪退
options = webdriver.ChromeOptions()
options.add_experimental_option('detach', True)
driver = webdriver.Chrome('./chromedriver/chromedriver', options=options) #指定chromedriver位置
#打开指定网站
driver.get(url1)
# 点击登录
driver.find_element_by_id('s-top-loginbtn').click()
#显示等待,presence_of_element_located的参数只有一个所以By.XPATH,'//*[@id="TANGRAM__PSP_11__userName"]',需要加上括号
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="TANGRAM__PSP_11__userName"]')))
#输入账号密码
driver.find_element_by_xpath("//*[@id='TANGRAM__PSP_11__userName']").send_keys('****')
driver.find_element_by_xpath("//*[@id='TANGRAM__PSP_11__password']").send_keys('*****')
driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__submit"]').click()
driver.close()
driver.quit()
open('https://www.baidu.com/')
selenium 百度登录
最新推荐文章于 2024-08-27 17:30:34 发布
该代码示例展示了如何使用Python的Selenium库在无头Chrome模式下打开网页,执行登录操作。通过设置ChromeOptions,避免了无界面模式下的错误,并实现了登录后的自动操作,如输入账号和密码,点击登录按钮。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Python3.8
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本
692

被折叠的 条评论
为什么被折叠?



