import time
from django.db import models
import pytest
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
'''
隐式等待:
# 设置隐式等待;
WebDriverWait(charm, 5).until(EC.element_to_be_clickable((By.ID, 'success_btn')))
WebDriverWait(charm, 5, 1)
charm 浏览器对象
5 等待时间,单位秒
1 轮询间隔
EC.element_to_be_clickable((By.ID, 'success_btn'))
注意 这一定要传元组(By.ID, 'success_btn')否则无法识别
1. 隐式等待,用于获取到元素后,与元素进行交互,得不到想要的 效果;
1.1 如.元素点击click()
2. 造成该问的原因: 因为页面在进行加载是是先加载页面元素,后加载js;从而导致;获取到的元素无法进行交互;
显示等待:
# 设置显示等待
charm.implicitly_wait(5)
1. 显示等待,用于页面渲染问题,导致find_element()找不元素问题;
2. 显示等待一旦设后,从设置的位置开始,生效;
3. 如何判断,获取不到元素,是因为页面加载慢,而不是其它原因;
3.1 在报错的代码前行设置一个强制等待:sleep(5)
3.2 如果能获取的元素,则说明是页面加载问题;否则是其它问题;
强制等待:
time.sleep()
'''
def get_chrome(url):
charm = webdriver.Chrome()
charm.implicitly_wait(5)
charm.get(url)
charm.maximize_window()
return charm
charm = get_chrome("https://vip.ceshiren.com/#/ui_study")
def test_get_element():
charm.find_element(By.XPATH, '//*[@id="locate_id"]/a/span').text
WebDriverWait(charm, 5).until(EC.element_to_be_clickable((By.ID, 'success_btn')))
charm.find_element(By.ID, 'success_btn').click()
if __name__ == '__main__':
test_get_element()