概述
**对于python 类来说,如果要调用类,就需要对类进行实例化**
第一步 环境的搭建
选择你要控制的浏览器,然后下载对应的驱动。
比如要控制google浏览器,就需要下载chromedriver驱动。
下载地址:http://chromedriver.storage.googleapis.com/index.html
根据电脑里面的google浏览器下载对应版本,然后将下载的包放入到python文件夹中。
python安装包的安装:pip install selenium
第二步 通过selenium来控制网页
在控制网页前你要确定你选用哪种,网上百度一下大概有八种。这里选用的是xpath定位。
进入google浏览器,进入开发者模式,通过箭头选择你想控制的区域,然后右键copy --》 xpath就可以获得该位置的xpath定位。
import configparser
from selenium import webdriver
import sys, os
from selenium.webdriver.support.ui import Select
WebStartAddress = "/html/body/div/div[2]/div[2]/input"
WebLoginAddress = "/html/body/div/div[2]/div[2]/span[2]"
WebSelectVehicleAdd = "/html/body/div/div[1]/div/form/div[1]/div/div[2]/select"
WebSelectFunctAdd = "/html/body/div/div[1]/div/form/div[1]/select[1]"
WebSelectTypeAdd = "/html/body/div/div[1]/div/form/div[1]/select[2]"
WebCheckOnlineAddress = "/html/body/div/div[1]/div/form/div[1]/span[4]/button"
WebCheckBoxAdd = '//*[@id="false"]'
WebClickMeAdd = '/html/body/div/div[1]/div/form/div[4]/button'
WebType = ["terminateService", "startService", "requestData", "responseData", "executionAction", "cancelaction", "queryAction"]
class Browser(object):
def __init__(self):
print("start")
# self.driver = webdriver.Chrome()
#打开浏览器
def open_browser(self):
config = configparser.ConfigParser()
filename = 'config.ini'
config.read(filename, encoding = 'utf-8')
browser = config.get("browserType", "browserName")
url = config.get("testServer", "URL")
if browser == "Chrome":
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.get(url)
return self.driver
def open_url(self, url):
self.driver.get(url)
def quit_browser(self):
self.driver.quit()
def forward(self):
slef.driver.forward()
def back(self):
self.driver.back()
def wait(self, seconds):
self.driver.implicitly_wait(seconds)
def input(self, x_path, text):
el = self.driver.find_element_by_xpath(x_path)
try:
el.clear()
el.send_keys(text)
except NameError as e:
print(e)
def click(self, x_path):
el = self.driver.find_element_by_xpath(x_path)
try:
el.click()
except NameError as e:
print(e)
def select_list(self, x_path, value):
el = Select(self.driver.find_element_by_xpath(x_path))
try:
el.select_by_visible_text(value)
except NameError as e:
print(e)
'''
if __name__ == "__main__":
webControl = Browser()
webControl.open_browser()
# Browser().quit_browser()
webControl.input(WebStartAddress, "lspa")
webControl.click(WebLoginAddress)
webControl.select_list(WebSelectVehicleAdd, "LSPA-464446")
#webControl.select_list(WebSelectFunctAdd, "rhl")
#webControl.select_list(WebSelectTypeAdd, WebType[1])
webControl.click(WebCheckOnlineAddress)
#webControl.click(WebCheckBoxAdd)
#webControl.click(WebClickMeAdd)
'''
最后
以上就是隐形石头为你收集整理的通过Selenium实现对网页的控制的全部内容,希望文章能够帮你解决通过Selenium实现对网页的控制所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复