python中执行input、但没让输入数据_selenium 输入表单数据 如何执行input中js代码...

模拟登陆校园网管理后台 账号密码输不上

from selenium import webdriver

url = "http://125.221.35.78:8080/selfservice/"

browser = webdriver.Firefox()

browser.get(url)

browser.switch_to_frame("entry.jsp")

username_element = browser.find_element_by_id('name_text')

username_element.clear()

username_element.send_keys('2016010387')

password_element = browser.find_element_by_id('password_text')

password_element.clear()

password_element.send_keys('123456')

执行完上述代码,发现页面账号和密码输入不上

查看一下网页源代码发现input里面包含javascript代码

那么问题来了?

如何使用python 爬虫执行这些js代码使其成功输入账号和密码并点击登录

在此先谢谢各位大大们!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用seleniuminput上传文件的Python代码示例: ```python from selenium import webdriver import os # 设置Chrome浏览器的驱动路径 chrome_driver_path = "path/to/chromedriver" # 创建Chrome浏览器实例 driver = webdriver.Chrome(chrome_driver_path) # 打开上传文件页面 driver.get("https://example.com/upload") # 点击上传文件按钮 upload_button = driver.find_element_by_id("upload-button") upload_button.click() # 构造文件路径 file_path = os.path.abspath("path/to/file") # 执行JavaScript脚本,模拟文件上传操作 script = f"document.querySelector('#upload-input').style.display = 'block'; document.querySelector('#upload-input').style.opacity = 1; document.querySelector('#upload-input').style.pointerEvents = 'auto'; document.querySelector('#upload-input').style.position = 'absolute'; document.querySelector('#upload-input').style.top = 0; document.querySelector('#upload-input').style.left = 0; document.querySelector('#upload-input').style.width = '100%'; document.querySelector('#upload-input').style.height = '100%'; document.querySelector('#upload-input').style.zIndex = 9999; document.querySelector('#upload-input').setAttribute('multiple', ''); document.querySelector('#upload-input').setAttribute('type', 'file'); document.querySelector('#upload-input').setAttribute('name', 'file'); document.querySelector('#upload-input').setAttribute('id', 'file'); document.querySelector('#file').setAttribute('onchange', 'document.querySelector(`#upload-form`).submit()'); document.querySelector('#file').click();" driver.execute_script(script) # 使用send_keys()方法发送文件路径 file_input = driver.find_element_by_id("file") file_input.send_keys(file_path) # 提交上传文件表单 upload_form = driver.find_element_by_id("upload-form") upload_form.submit() # 关闭浏览器实例 driver.quit() ``` 以上代码,使用JavaScript脚本模拟了点击上传文件按钮、显示文件选择框、设置文件属性等操作,然后使用send_keys()方法发送文件路径,最后提交上传文件表单。注意,此方法仅适用于一些特定的上传文件页面,具体实现方式可能会有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值