在不使用任何二次封装的情况下,很多时候必须写一行元素等待,再进行操作,并且代码看上去实在是很长。比如一个简单的登陆操作,得写这么一大堆: 这还是直接有id定位+强制等待的情况下,同样的功能,如果我们只有css/xpath可以参考,并且使用隐式/显示等待,那代码就会更长更丑(虽然不难读懂): 不管使用哪种,都是等元素,找元素,操作元素。要写三句啊很麻烦有木有!下面直接上我的封装代码: 这样我再在这个class里写登陆function的时候,只需要: 简洁哭了有木有!!!from selenium import webdriver from time import sleep def loginWebsite(): driver.get('www.xxx.com') driver.maximize_window() username = 'username' password = 'password' sleep(3) driver.find_element_by_id("dropdown-account").click() sleep(3) driver.find_element_by_id("login").send_keys(username) driver.find_element_by_id("login").submit() sleep(3) driver.find_element_by_id("password").send_keys(password) driver.find_element_by_id("password").submit()
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 def login(): driver.get('www.xxx.com') driver.implicitly_wait(10) try: driver.find_element_by_xpath('//*[@id="secondary-block"]/div[1]/div/div/div[1]/label').click() except NoSuchElementException as e: print(e) #隐式等待10s WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'#logoin-page > div > div > section > input'),message='') driver.find_element_by_css_selector('#order-detail-page > div > div > section > p').send_keys(username) driver.find_element_by_css_selector('#order-detail-page > div > div > section > p').submit() WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="secondary-block"]/div[1]/div/div/div[2]'),message='') driver.find_element_by_xpath('//*[@id="secondary-block"]/div[1]/div/div/div[2]').send_keys(password) driver.find_element_by_xpath('//*[@id="secondary-block"]/div[1]/div/div/div[2]').send_keys(submit) #显示等待10s #presence_of_element_located判断某个元素是否被加到了dom 树里
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 class A: def __init__(self): self.driver = webdriver.Chrome('D:/chromedriver.exe') def find(self,method,value,secs=20): wait = WebDriverWait(self.driver, secs) try: if method == 'id': wait.until(EC.presence_of_element_located((By.ID, value)))#presence_of_element_located省略了message字段必须有两层括号 element = self.driver.find_element_by_id(value) elif method == 'name': wait.until(EC.presence_of_element_located((By.NAME, value))) element = self.driver.find_element_by_name(value) elif method == 'class': wait.until(EC.presence_of_element_located((By.CLASS_NAME, value))) element = self.driver.find_element_by_class_name(value) elif method == 'linktext': wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, value))) element = self.driver.find_element_by_partial_link_text(value) elif method == 'xpath': element = self.driver.find_element_by_xpath(value) wait.until(EC.presence_of_element_located((By.XPATH, value))) elif method == 'css': wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, value))) element = self.driver.find_element_by_css_selector(value) else: print("语法错误") except BaseException as e: print(e) return element
def login(self,username,password): self.find('id','dropdown-account').click() self.find('id','login').send_keys(username) self.find('id','login').submit() self.find('id','password').send_keys(password) self.find('id','password').submit()
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算