browser.find_element(By.ID, key )
时间: 2024-04-30 20:18:32 浏览: 9
这是 Python 的 Selenium 库中的代码,用于通过 ID 查找网页元素。其中,By.ID 表示通过 ID 查找元素,key 是要查找的元素的 ID 值,可以根据需要替换成其他属性或方式来查找元素。例如,通过 class name 查找元素的代码为:
```
browser.find_element(By.CLASS_NAME, class_name)
```
相关问题
from selenium import webdriver from time import sleep from aip import AipOcr import re APP_ID = '32948455' API_KEY = 'Ojs6MOUb8b19VVqiVHhEckgH' SECRET_KEY = 'xV7i3uPcugk0DB3GdonK4cgiqMiGt6vd' client = AipOcr(APP_ID,API_KEY,SECRET_KEY) browser = webdriver.Chrome('D:\Pycharm\PyCharm 2020.3.5\chromedriver.exe') browser.get('http://jwcmis.hnie.edu.cn/') username = '' password = '' #browser.find_element_by_id('username').click() browser.find_element_by_id('userAccount').send_keys(username) sleep(1) #browser.find_element_by_id('password').click() browser.find_element_by_id('userPassword').send_keys(password) sleep(1) ele_vcode = browser.find_element_by_id('SafeCodeImg') ele_vcode.click() sleep(1) ele_vcode.screenshot('vcode3.png') with open(r'D:\Pycharm\PyCharm 2020.3.5\pythonProject\venv\Scripts\vcode3.png','rb') as f: image = f.read() data = str(client.basicGeneral(image)).replace(" ","") pat = re.compile(r"{'words':'(.*?)'}") result = pat.findall(data)[0] browser.find_element_by_id('RANDOMCODE').send_keys(result) browser.find_element_by_id('btn-login').click()解析这段代码
这段代码是一个自动化脚本,用于模拟用户登录教务系统。主要使用了selenium和百度AI的OCR识别技术。具体步骤如下:
1. 导入必要的库:selenium、time、aip
2. 初始化百度AI的OCR客户端
3. 启动Chrome浏览器,并打开教务系统登录页面
4. 输入用户名和密码
5. 获取验证码图片元素,并截图保存到本地
6. 读取验证码图片,并使用OCR技术识别验证码
7. 将识别结果填入验证码输入框
8. 点击登录按钮,完成登录操作
import re import json import time import requests import datetime import pymysql import selenium from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from sqlalchemy import create_engine, Column, Integer, String, Text, DateTime from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base from selenium.webdriver import Edge, EdgeOptions # 创建浏览器对象 options = EdgeOptions() options.use_chromium = True options.binary_location = r'C:\Users\邓枫林\PycharmProjects\pythonProject\edgedriver_win64\msedgedriver.exe' browser = Edge(options=options) wait = WebDriverWait(browser, 10) # 打开微博话题页面 url = 'https://weibo.com/n/%E4%B8%AD%E5%9B%BD%E9%A3%9F%E5%93%81%E5%8D%AB%E7%94%9F?from=feed&loc=at&nick=%E4%B8%AD%E5%9B%BD%E9%A3%9F%E5%93%81%E5%8D%AB%E7%94%9F&order=hot' browser.get(url) # 等待页面加载完成 wait.until(lambda driver: driver.execute_script("return document.readyState") == "complete") browser = selenium.webdriver.Edge(executable_path='C:/Users/邓枫林/PycharmProjects/pythonProject/edgedriver_win64/msedgedriver.exe') wait = selenium.webdriver.support.ui.WebDriverWait(browser, 10) # 监测页面是否包含“高校类”敏感词汇 if '高校类' in browser.page_source: # 获取原始微博 weibo = browser.find_element_by_css_selector('.WB_feed_detail .WB_text.W_f14').text # 获取转发该微博的用户昵称和转发内容 reposts = [] repost_items = browser.find_elements_by_css_selector('.list_ul .list_li') for item in repost_items: nickname = item.find_element_by_css_selector('.WB_text.W_f14').text content = item.find_element_by_css_selector('.WB_text.W_f14 + .comment_txt').text reposts.append({'nickname': nickname, 'content': content}) # 关闭浏览器 browser.quit() # 将微博和转发内容存入MySQL数据库中 Base = declarative_base() class Weibo(Base): tablename = 'weibo_user' id = Column(Integer, primary_key=True) content = Column(Text) create_time = Column(DateTime) class Repost(Base): tablename = 'weibo_repost' id = Column(Integer, primary_key=True) weibo_id = Column(Integer) nickname = Column(String(50)) content = Column(Text) engine = create_engine('mysql+pymysql://root:root@hostname:port/weibo?charset=utf8mb4') Session = sessionmaker(bind=engine) session = Session() now = datetime.datetime.now() weibo_obj = Weibo(content=weibo, create_time=now) session.add(weibo_obj) session.commit() for repost in reposts: repost_obj = Repost(weibo_id=weibo_obj.id, nickname=repost['nickname'], content=repost['content']) session.add(repost_obj) session.commit() session.close() else: # 关闭浏览器 browser.quit()
这段代码是一个 Python 程序,使用了 Selenium 库模拟浏览器行为,从微博话题页面中获取微博和转发内容,并将它们存入 MySQL 数据库中。其中,程序打开了一个 Microsoft Edge 浏览器对象,等待页面加载完成后进行操作。如果页面包含“高校类”敏感词汇,则获取微博和转发内容,并将它们存入数据库。如果页面不包含该敏感词汇,则直接关闭浏览器。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)