from selenium import webdriver class Browser(): def __init__(self, webdriver): self.driver = webdriver #打开浏览器 def get(self, url): self.driver.get(url) def quit(self): self.driver.quit() #根据id查找元素 def findId(self, id): find = self.driver.find_element_by_id(id) return find #根据名称查找元素 def findName(self, name): find = self.driver.find_element_by_name(name) return find #根据类名查找元素。 def findClassName(self, className): find = self.driver.find_element_by_class_name(className) return find #根据Tag Name查找元素 def findTagName(self, tagName): find = self.driver.find_element_by_tag_name(tagName) return find def findLinkText(self, text): find = self.driver.find_element_by_link_text(text) return find def findPartialLinkText(self, text): find = self.driver.find_element_by_partial_link_text(text) return find def findXPath(self, xpath): find = self.driver.find_element_by_xpath(xpath) return find #根据CSS选择器查找元素。 def findCSS(self, css): find = self.driver.find_element_by_css_selector(css) return find #多个元素的查找 # 根据id查找元素 def findAllId(self, id): find = self.driver.find_elements_by_id(id) return find # 根据名称查找元素 def findAllName(self, name): find = self.driver.find_elements_by_name(name) return find # 根据类名查找元素。 def findAllClassName(self, className): find = self.driver.find_elements_by_class_name(className) return find # 根据Tag Name查找元素 def findAllTagName(self, tagName): find = self.driver.find_elements_by_tag_name(tagName) return find def findAllLinkText(self, text): find = self.driver.find_elements_by_link_text(text) return find def findAllPartialLinkText(self, text): find = self.driver.find_elements_by_partial_link_text(text) return find def findAllXPath(self, xpath): find = self.driver.find_elements_by_xpath(xpath) return find # 根据CSS选择器查找元素。 def findAllCSS(self, css): find = self.driver.find_elements_by_css_selector(css) return find
#后面是测试代码
from selenium import webdriver from browser import Browser from time import sleep browser = Browser(webdriver.Firefox()) browser.get("https://www.baidu.com") browser.findId("kw").send_keys("python selenium") browser.findId("su").click() sleep(2) browser.quit()