"""
1、企业基本信息
2、http://mobile.chinaz.com/mft.html?url=www.dlyqgs.com 检查每个网站的移动兼容性
"""
import csv
from lxml import etree
from redis import Redis
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import time
from random import randint
# chromeOptions = webdriver.ChromeOptions()
# chromeOptions.add_argument("--proxy-server=http://103.231.146.213:55182")
# # 一定要注意,=两边不能有空格,不能是这样--proxy-server = http://202.20.16.82:10152
# browser = webdriver.Chrome(chrome_options = chromeOptions)
browser = webdriver.Chrome()
wait = WebDriverWait(browser,10)
if __name__ == '__main__':
try:
browser.get('http://mobile.chinaz.com/mft.html?url=www.dlyqgs.com')
num = 0
with open('dalian_all.csv', 'rt', encoding='gbk') as csvfile:
reader = csv.DictReader(csvfile)
for line in reader:
num += 1
if num > 44:
urls = [row['url'] for row in reader]
for url in urls:#//*[@id="inputURL"]
# 输入url
input_url = wait.until(
EC.presence_of_element_located((By.XPATH, '//*[@id="inputURL"]'))
)
input_url.clear()
input_url.send_keys(url)
# 分析
search_key = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="btnCMF"]')))
search_key.click()
# 判断页面是否加载成功
wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="btnCMF"]')))
# time.sleep(randint(1,10))
time.sleep(10)
html = browser.page_source
response = etree.HTML(html)
results = response.xpath('/html/body/div[2]/div[2]/div/div[1]/ul/li[1]')
with open('result.csv', 'a', encoding='utf-8',newline='') as fl:
# fl.write('url' + ',' + 'result' + '\n')
for result in results:
result = result.xpath('text()')
result = result[0]
print(result)
fl.write(url)
fl.write(',')
fl.write(result)
fl.write('\n')
# fl.close()
except StaleElementReferenceException as msg:
print('异常%s'%msg)
使用selenium检查网址的兼容性
最新推荐文章于 2024-05-11 15:53:59 发布
本文介绍了一种使用Python Selenium和Chromedriver自动化测试网站移动兼容性的方法。通过读取CSV文件中的URL列表,该脚本将每个URL输入到ChinaZ的移动友好测试工具中,并抓取测试结果,最后将结果保存到新的CSV文件中。
摘要由CSDN通过智能技术生成