使用selenium检查网址的兼容性

本文介绍了一种使用Python Selenium和Chromedriver自动化测试网站移动兼容性的方法。通过读取CSV文件中的URL列表,该脚本将每个URL输入到ChinaZ的移动友好测试工具中,并抓取测试结果,最后将结果保存到新的CSV文件中。
摘要由CSDN通过智能技术生成
"""
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)




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值