栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

selenium自动化测试工具python笔试面试项目实战6 javascript操作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

selenium自动化测试工具python笔试面试项目实战6 javascript操作

图片.png

参考答案

#!/usr/bin/python3# -*- coding: utf-8 -*-# 讨论钉钉免费群21745728 qq群144081101 567351477# CreateDate: 2018-10-20
 from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get('http://example.webscraping.com/places/default/search')
driver.find_element_by_id('search_term').send_keys('.')
js = "document.getElementById('page_size').options[1].text = '300';"driver.execute_script(js)
driver.find_element_by_id('search').click()
links = driver.find_elements_by_css_selector('#results a')
countries = [link.text for link in links]
print(len(countries))
print(countries)

driver.close()

本例参考书籍:用Python写网络爬虫.pdf

selenium 上机实操: 下拉刷新框中所有内容(javascript实现)
  • 打开:http://www.webscrapingfordatascience.com/complexjavascript/

  • 抓取框中所有内容。该框按住鼠标中键下滚,会有刷新内容,直至完全加载

图片.png

参考答案

#!/usr/bin/python3# -*- coding: utf-8 -*-# 讨论钉钉免费群21745728 qq群144081101 567351477# CreateDate: 2018-10-18from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.common.exceptions import TimeoutExceptionclass at_least_n_elements_found(object):
    def __init__(self, locator, n):
        self.locator = locator
        self.n = n    def __call__(self, driver):
        elements = driver.find_elements(*self.locator)        if len(elements) >= self.n:            return elements        else:            return False
        url = 'http://www.webscrapingfordatascience.com/complexjavascript/'driver = webdriver.Chrome()
driver.get(url)# Use an implicit wait for cases where we don't use an explicit onedriver.implicitly_wait(10)
div_element = driver.find_element_by_class_name('infinite-scroll')
quotes_locator = (By.CSS_SELECTOR, ".quote:not(.decode)")
nr_quotes = 0while True:    # Scroll down to the bottom
    driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight',
                          div_element)    
    # Try to fetch at least nr_quotes+1 quotes
    try:
        all_quotes = WebDriverWait(driver, 3).until(
            at_least_n_elements_found(quotes_locator, nr_quotes + 1))    except TimeoutException as ex:        # No new quotes found within 3 seconds, assume this is all there is
        print("... done!")        break
    
    # Otherwise, update the quote counter
    nr_quotes = len(all_quotes)
    print("... now seeing", nr_quotes, "quotes")    
# all_quotes will contain all the quote elementsprint(len(all_quotes), 'quotes foundn')for quote in all_quotes:
    print(quote.text)
input('Press ENTER to close the automated browser')
driver.quit()



作者:python作业AI毕业设计
链接:https://www.jianshu.com/p/62afcd9ec11d


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/222354.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号