self_example/Spider/Chapter07_动态渲染页面爬取/seleniumLearning/demo8前进后退.py

31 lines
1.2 KiB
Python

# -*- encoding:utf-8 -*-
'''
@Author : dingjiawen
@Date : 2023/12/6 20:38
@Usage : 延时等待
@Desc :get方法在网页框架加载结束后才会结束执行
所以在get方法执行完毕之后其结果可能并不是浏览器完全加载完成的页面
所以在必要时我们需要设置浏览器延时等待一段时间
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
# 隐式等待 :效果并不好,因为只规定了一个固定时间,页面加载事件会受到网络条件影响
browser.implicitly_wait(10)
browser.get('https://spa2.scrape.center/')
input = browser.find_element(By.CLASS_NAME, 'logo-image')
print(input)
# 显示等待:指定要查找的节点和最长等待时间
browser.get('https://www.taobao.com/')
wait = WebDriverWait(browser, 10)
# presence_of_element_located这个条件表示节点出现
input = wait.until(EC.presence_of_element_located((By.ID, 'q')))
# element_to_be_clickable表示按钮可点击
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search')))
print(input, button)