# -*- coding: utf-8 -*-
"""
@author: Dell Created on Tue Dec 24 12:33:56 2019
"""
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait#等待一个元素加载完成
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys#键盘
from selenium.webdriver.common.action_chains import ActionChains#鼠标
driver= webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.implicitly_wait(10)#控制操作的时间在10s以内,隐式等待,很少用
# driver.find_element_by_id("kw").send_keys("selenium")
#打开一个网页,15s超时,每0.5s检测一次,直到id为kw的元素出现,
#节约时间,显示等待15s,必须等待这个元素出现,若15s之后还没出现才会不等待了
WebDriverWait(driver, 15, 0.5).until(EC.presence_of_element_located((By.ID,"kw"))).send_keys("selenium")#
driver.find_element_by_id("su").click()#点击百度一下进行搜索
nums_span = driver.find_element_by_class_name("nums_text")
if nums_span.is_displayed():#判断这个元素是否已经显示出来
print(nums_span.text)
time.sleep(5)
driver.close()
if __name__ == "__main__":
pass
原文地址:https://www.cnblogs.com/zxfei/p/12090897.html
时间: 2024-11-10 13:33:25