一,介绍
Selenium是一个强大的开源Web功能测试工具系列,可进行读入测试套件、执行测试和记录测试结果,模拟真实用户操作,包括浏览页面、点击链接、输入文字、提交表单、触发鼠标事件等操作,并且能够对页面结果进行种种验证
二,环境准备
1,安装selenium
可以使用pip install seleniu进行安装,这里演示用工具pycharm安装
在PyCharm中file->setting,找到project->project interpreter,点击添加
在弹出的页面中
2,驱动安装
1.chromedriver的驱动下载地址:https://code.google.com/p/chromedriver/downloads/list
2.Firefox的驱动geckodriver下载地址:https://github.com/mozilla/geckodriver/releases/
下载解压到python目录即可
三、简单验证
http://m.credit100.com/#/redBlackList?type=3 这个页面是新华社重大税收违法案件当事人查询页面
查询具体某个企业新详情页面测试代码:
#-*- coding: UTF-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as BS
compy_name='黑山县益盛药业有限公司'
compy_id='7693'
url="http://m.credit100.com/#/redBlackText?name="+compy_name+"&code="+compy_id+"&type=3"
# driver = webdriver.Chrome("D:/python36/chromedriver.exe")
#driver = webdriver.Firefox("D:\python36\geckodriver.exe")
driver = webdriver.Firefox()
driver.implicitly_wait(30) # 等待
driver.get(url) # 访问地址
time.sleep(6)
# 公司
compy_Name_xpath = "/html/body/app-root/app-red-black-text/div/div/div/div/div/div[1]/div/h3"
#组织机构代码
org_xpath='/html/body/app-root/app-red-black-text/div/div/div/div/div/div[2]/ul/li[1]/h4'
#组织机构代码值
org_value_xpath='/html/body/app-root/app-red-black-text/div/div/div/div/div/div[2]/ul/li[1]/p'
compy_name = driver.find_element_by_xpath(compy_Name_xpath).text
org = driver.find_element_by_xpath(org_xpath).text
org_code = driver.find_element_by_xpath(org_value_xpath).text
#print
print(compy_name)
print(org)
print(org_code)
如果想详细看看具体的api,请参考https://selenium-python-zh.readthedocs.io/en/latest/