webdriver

selenium自动化爬虫测试

对着背影说爱祢 提交于 2020-03-02 19:58:03
import time from selenium import webdriver from lxml import etree from selenium.webdriver import ActionChains browser = webdriver.Chrome() url = "http://www.baidu.com" browser.get(url) title = browser.find_element_by_xpath('//*[@id="su"]') print(title.get_attribute("value")) # time.sleep(2) # input = browser.find_element_by_css_selector('#kw') # input.send_keys('韩国女团') # time.sleep(2) # input.clear() # input.send_keys('后背摇') # button = browser.find_element_by_css_selector('#su') # button.click() # time.sleep(10) browser.close() # url = "http://www.runoob.com/try/try.php?filename=jqueryui-api

Appium + Python自动化测试学习之三:元素等待机制

大憨熊 提交于 2020-03-01 17:52:56
在我们自动化过程中,能否构建一个健壮和可靠的测试是UI自动化测试能否成功的关键因素之一。然而在自动化过程中试着去执行的时候,常常会出现各种不同的状况,当使用脚本定位元素或者去验证程序的运行状态时,有时候会发现找不到元素,这可能是由于突然的资源受限或者网络延迟或者机器性能等各种因素引起的响应速度太慢导致的,这时候测试报告就会返回测试失败的结果。其实元素是正常加载的,只是加载的时间晚了一点,那么遇到这种情况我们该怎么解决呢? 我们需要在测试脚本中引入延时机制,来使脚本的运行速度与程序元素的加载速度匹配。通俗意义上讲,就是我们需要使脚本和程序的响应能够同步。 我们有三种等待机制,强制等待、隐式等待和显式等待。 如何使用隐式等待或显式等待? 在什么情况下使用隐式等待或显式等待? 什么情况下使用强制等待? 一.隐式等待 隐式等待是通过一定的时长等待页面上某元素加载完成。如果超出了设置的时长元素还没有加载完成,则会抛出NoSuchElementException的异常。 一旦设置,隐式等待时间就会作用于这个webdriver实例的整个周期或者一次完整测试的执行周期,并且webdriver会使其对所有测试步骤中包含整个界面的元素的查找时都有效,除非把默认时间设置回0。 webdriver提供了implicitly_wait()方法来实现隐式等待,默认参数是以秒为单位。如下实例: from

用法详解

江枫思渺然 提交于 2020-03-01 13:41:13
一、环境搭建参考: https://blog.csdn.net/efly2333/article/details/80346426 二、selenium用法详解( https://www.cnblogs.com/themost/p/6900852.html ) 1 selenium用法详解 2 selenium主要是用来做自动化测试,支持多种浏览器,爬虫中主要用来解决JavaScript渲染问题。 3 模拟浏览器进行网页加载,当requests,urllib无法正常获取网页内容的时候 4 5 一、声明浏览器对象 6 注意点一,Python文件名或者包名不要命名为selenium,会导致无法导入 7 from selenium import webdriver 8 #webdriver可以认为是浏览器的驱动器,要驱动浏览器必须用到webdriver,支持多种浏览器,这里以Chrome为例 9 browser = webdriver.Chrome() 10 11 二、访问页面并获取网页html 12 from selenium import webdriver 13 browser = webdriver.Chrome() 14 browser.get('https://www.taobao.com') 15 print(browser.page_source)#browser

Python+selenium之下载文件

对着背影说爱祢 提交于 2020-03-01 03:16:10
一、Firefox文件下载 Web容许我们设置默认的文件下载路劲,文件会自动下载并且存放在指定的目录下。 1 from selenium import webdriver 2 import os 3 fp = webdriver.FirefoxProfile() 4 fp.set_preference("browser.download.folderList",0) 5 fp.set_preference("browser.download.manager.showhenStarting",True) 6 fp.set_preference("browser.download.dir",os.getcwd()) 7 fp.set_preference("browser.helperApps.neverAsk.saveToDisk","applaction/octet-stream")#下载文件类型 8 9 driver = webdriver.Firefox(firefox_profile = fp) 10 driver.get("http://pypi.Python.org/pypi/selenium") 11 driver.find_element_by_xpath("//*[@id='download-button']/a").click() 12 13 driver

selenium3源码解析Python篇(二)-异常类

試著忘記壹切 提交于 2020-02-29 20:34:50
本篇所讲的是关于selenium源码中的异常类 通过源码我们可以得知selenium的异常有很多 ErrorInResponseException InvalidSwitchToTargetException NoSuchFrameException NoSuchWindowException NoSuchElementException NoSuchAttributeException …… 以NoSuchElementException为例: 源码是这么描述的: 由此我们可以了解到,遇到此种错误,我们需要去检查以下几项: 1.您在查找中使用的选择器 2.元素在查找操作时可能尚未出现在屏幕上 3.网页仍在加载,如何编写等待包装器以等待元素出现 接下来我们介绍下selenium webdriver中常见的异常: NoSuchElementException 如上例子所属,属于没找到元素异常 NoSuchFrameException 没找到iframe异常 NoSuchWindowException 没找到窗口句柄异常 NoSuchAttributeException 属性错误异常(id=“kw” id为元素,kw为属性) NoAlertPresentException 没找到alert弹出框异常 ElementNotVisibleException 元素不可见异常

Use selenium with C

ε祈祈猫儿з 提交于 2020-02-29 06:42:28
问题 I can't find any information on if you can use selenium with C. Just C, not c#. Does anyone know if this is possible? I tried searching on Google but all results that appear has "C#" in it and not "C". 回答1: No. Here is a list of official Selenium bindings, and here is a list of unofficial selenium bindings. C is not listed on either. 回答2: From the official page of Selenium: The core language-specific client drivers are: Ruby JavaScript Java Python C# However as per Selenium Official Home Page

Selenium Python使用技巧(三)

早过忘川 提交于 2020-02-28 02:10:48
书接上文和上上文: Selenium Python使用技巧(一) Selenium Python使用技巧(二) 处理不同情况的等待 在Selenium自动化测试中网页可能需要花费一些时间来加载,或者希望在触发测试代码之前可以看到页面上的特定Web元素。在这种情况下,需要执行“显式等待”,这是一段代码,通过它可以定义要发生的条件,然后再继续执行代码。 Selenium具有 WebDriverWait ,可以将其应用于任何具有条件和持续时间的Web元素。如果不存在执行等待的元素或发生超时,则可能引发异常。 在下面的示例中,我们等待 link_text=Sitemap 加载到页面上,并在 WebDriverWait 方法中指定了超时。如果在超时时间内未加载该元素,则抛出异常。 from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By from pip._vendor.distlib import resources from selenium.webdriver

selenium环境安装

你离开我真会死。 提交于 2020-02-27 19:05:32
1:下载selemium。cmd中pip install selenium 2:下载对应的浏览器驱动。谷歌:chromedriver.exe 浏览器驱动放到python安装目录下。 问题:使用驱动过程出现如下问题,是因为下载下来的驱动没有解压,将下载下来的压缩文件解压后放到python安装目录中就可以了。 Traceback (most recent call last): File “C:\SoftwareSetup\python3.7.1\lib\site-packages\selenium\webdriver\common\service.py”, line 76, in start stdin=PIPE) File “C:\SoftwareSetup\python3.7.1\lib\subprocess.py”, line 769, in init restore_signals, start_new_session) File “C:\SoftwareSetup\python3.7.1\lib\subprocess.py”, line 1172, in _execute_child startupinfo) FileNotFoundError: [WinError 2] 系统找不到指定的文件。 During handling of the above exception,

使用webdriver打开本地浏览器--python版

本秂侑毒 提交于 2020-02-27 09:02:20
背景:经常性的,在项目中我们需要打开不同配置的不同浏览器。在学习selenium的过程中,打开本地火狐和本地chrome是一个稍微麻烦的事情,网上的java版本资料很多,但是python版的不多,在这里,我研究了一份关于python版Selenium打开浏览器的文档,供自己备注,也希望给大家一些参考。 1.打开默认的火狐 browser = webdriver.Firefox() 2.打开本地配置的火狐 from selenium import webdriver from time import sleep import os profileDir = “C:Users\\cui\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\vrpxe102.default” profile = webdriver.FirefoxProfile(profileDir) browser = webdriver.Firefox(profile) 解析: 第一部分是给出本地的配置地址,这个地址在火狐浏览器的安装目录下,相对路径即为Firefox\\Profiles\\vrpxe102.default。也就是 .default文件。各人的浏览器下这个.default文件的名字不一样。 第二部分,即是读取本地配置。第三部分,即是利用这个本地的配置

python+selenium爬取京东数据

不羁的心 提交于 2020-02-27 04:02:51
问题描述 分别爬取京东和淘宝手机频道,找出累积销量 (所有商家销售同一 型号手机的销量之和)最高20款手机 。 说明 : 销量:京东以评论数为准,淘宝以付款人数为准 。 一款手机只看最低配置和价格,例如iPhone 11只按照 64G价格来计算。 环境&工具 python3.7 selenium Google Chrome开发版(启动selenium后自动弹出) 解决方案概述 利用网页的xpath进行爬取,边爬取边处理数据,共爬取37页,每页60部手机,写入”jingdong_rowdata.csv”。 导入包,初始化全局变量 from selenium import webdriver from selenium . webdriver . chrome . options import Options from selenium . common . exceptions import NoSuchElementException from selenium . common . exceptions import ElementClickInterceptedException from selenium . common . exceptions import ElementNotInteractableException import pandas as pd