show | version | enable_checker |
---|---|---|
step |
1.0 |
true |
- 上次我们尝试用selenium访问了一个网页
- 原理上selenium和requests发请求差不多
- 都是发送请求
- 只不过requests是直接发送请求
- selenium用真实的浏览器发送请求
- 可以动态加载一些后台的一些数据
- 得到网页源文件之后都可以xpath进行筛选提取
- 用selenium还可以做一些什么浏览器才能做的东西么?
- 可以通过selenium直接找到元素
- 然后进行一些键盘输入
- 最后点击按钮
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium import webdriver
from lxml import etree
import time
service = Service(executable_path="geckodriver")
driver = webdriver.Firefox(service=service)
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
text_box = driver.find_element(by=By.NAME, value="my-text")
text_box.send_keys("Selenium")
time.sleep(5)
driver.quit()
- 录入成功
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium import webdriver
from lxml import etree
import time
service = Service(executable_path="geckodriver")
driver = webdriver.Firefox(service=service)
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
text_box = driver.find_element(by=By.NAME, value="my-text")
text_box.send_keys("Selenium")
submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")
submit_button.click()
time.sleep(5)
driver.quit()
- 确实可以提交
- 提交之后可以对返回页进行判断么?
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium import webdriver
from lxml import etree
import time
service = Service(executable_path="geckodriver")
driver = webdriver.Firefox(service=service)
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
text_box = driver.find_element(by=By.NAME, value="my-text")
text_box.send_keys("Selenium")
submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")
submit_button.click()
message = driver.find_element(by=By.ID, value="message")
value = message.text
assert value == "Received!"
time.sleep(5)
driver.quit()
- 提交后driver再去查找元素
- 就在提交页里面查找了
- 可以不点击按钮
- 而按照回车的方式提交么?
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium import webdriver
from lxml import etree
import time
service = Service(executable_path="geckodriver")
driver = webdriver.Firefox(service=service)
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
text_box = driver.find_element(by=By.NAME, value="my-text")
text_box.send_keys("Selenium")
text_box.send_keys(Keys.ENTER)
time.sleep(0.5)#延迟时间等待响应
message = driver.find_element(by=By.ID, value="message")
value = message.text
assert value == "Received!"
time.sleep(5)
driver.quit()
- 需要导入一个Keys
- 确实可以发送回车到文本框
- 然后提交
- 我们可以那这一套用到百度上么?
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium import webdriver
from lxml import etree
import time
service = Service(executable_path="geckodriver")
driver = webdriver.Firefox(service=service)
driver.get("http://baidu.com")
text_box = driver.find_element(by=By.NAME, value="wd")
text_box.send_keys("Selenium")
text_box.send_keys(Keys.ENTER)
time.sleep(0.5)#延迟时间等待响应
message = driver.title
print(message)
time.sleep(5)
driver.quit()
- 确实可以搜索到
- 我们这次使用selenium模拟了浏览器的行为
- 模拟了填写表单的过程
- 可以填写表单加回车
- 也可以填写表单点击按钮
- 很多日常的填报工作可以用selenium的方式来填报提交
- 大幅提高效率
- 我们下次玩点什么呢?🤔
- 下次再说👋