Selenium自动填写带有登录的网页表单信息

selenium模拟人填写表单

所需环境

  1. selenium
  2. chromedriver_py

第一步 连接chrome驱动

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from chromedriver_py import binary_path
from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By
from selenium.webdriver.suppert.select import Select 
service = Service(executable_path=binary_path)
options = webdriver.ChromeOptions()
#它会根据自己电脑的chrome版本匹配对应的chrome驱动
diver=webdriver.Chrome(service=service,options=options)

第二步 登录所需要填写表单的网站


diver.get("登录网站地址")
# 根据html中的标签属性抓取到填写用户名的位置,输入用户名
diver.find_element("name","username").send_keys(username)
# 输入密码
diver.find_element("name","password").send_keys(password)
#根据XPATH匹配对应标签的位置,//标签名[@type=属性值],进行登录
diver.find_element(By.XPATH,"//input[@type='submit']").click() 
diver.implicitly_wait(10) #等待10s检查是否登录成功

第三步 填写表单

#跳转到填写表单的网站
diver.get("填写表单的网站" )
#找到表单位置并填写相应数据  填写的数据data记录在一个数据类型为dataFrame里
diver.find_elesent ("name","domain").send_keys(data.iloc[1]["domain"])
## 填写Addr:
diver.find_element("name", "Addr").send_keys(data.iloc[1]['Addr' ])
# 填写带有下拉框的选项
elen_radio = diver.find_element("id", "selector")
# 点击第二个框 
index = 1
#选择某个选项
Select(elen_radio).select_by_index(index)
# 填写Group:
diver.find_element("name", "Group").send_kays(data.iloc[i]['Group' ])
#填写Assignment
diver.find_element("name", "gsdGroup").send_keys(data.iloc[i]['Assignment'])
# 填写Sub
diver.find_element("name","subset0r0rigin").send_keys (data.loc[i]['Sub'])
#填写jobname
diver.find_element("nane","jobname"),send_keys (data.itoc[i]['Job name'])
#找到提交按钮,点击提交
WebDriverWait(diver, 58).until(EC.visibility_of_element_located(By.XPATH,
"//input[@id='form2-submit']'))).click()
#等待
diver.implicitly_wait(40)
#叉掉弹窗
WebDriverWait(diver, 78).until(
EC.element_to_be_clickable((By.XPATH,*//span[@class='modal-close']*))).click() 
#等待
diver.implicitly_wait(50)
#关闭浏览器
diver.quit()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Selenium中,如果要打开需要选择证书的网页,可以使用Firefox或Chrome浏览器,并在浏览器设置中指定默认证书。 对于Firefox浏览器,可以在安装目录下找到“cert8.db”文件,该文件包含默认证书。可以使用以下代码将其拷贝到Selenium使用的Firefox浏览器配置文件中: ``` from shutil import copyfile import os # Firefox profile directory profile = webdriver.FirefoxProfile('/path/to/firefox/profile') # Source certificate file cert_file = '/path/to/cert8.db' # Destination certificate file dest_cert_file = os.path.join(profile.path, 'cert8.db') # Copy certificate file to destination copyfile(cert_file, dest_cert_file) # Launch Firefox with profile driver = webdriver.Firefox(firefox_profile=profile) ``` 对于Chrome浏览器,可以在浏览器的高级设置中导入证书,并在Selenium中指定ChromeOptions以使用该证书。可以使用以下代码: ``` from selenium import webdriver # Path to the certificate file cert_file = '/path/to/cert.pfx' # Chrome options options = webdriver.ChromeOptions() # Add certificate to options options.add_argument('--auto-select-certificate-for-urls=https://example.com') options.add_argument('--certificate-file=%s' % cert_file) options.add_argument('--certificate-password=your_password') # Launch Chrome with options driver = webdriver.Chrome(chrome_options=options) ``` 在这里,`--auto-select-certificate-for-urls`选项指定需要选择证书的URL,`--certificate-file`选项指定证书文件的路径,`--certificate-password`选项指定证书密码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值