webdriver

Selenium-设置等待时间

99封情书 提交于 2020-01-23 11:17:28
参考文章链接:http://www.jb51.net/article/92684.htm 感谢分享 1. 强制等待 :通过Python中time包提供的sleep()方法设置固定的等待时间 2. 隐式等待 :其实就相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。如果页面元素已经出现,但页面未加载完,其仍会继续等待,直到页面加载完成。隐式地等待一个无素被发现或一个命令完成,这个方法每次会话只需要调用一次,最大设置30秒 语法:driver.implicitly_wait(30) 3. 显式等待 :通过WebDriverWait类和该类的until()方法配合,实现灵活的等待。它的执行过程是,首先设置一个超时时间,程序每隔一段时间查看一次页面,知道until()方法中的条件成立,则执行下一步,否则继续等待,直到超过设置的超时时间,然后抛出异常TimeoutException。 这个类的路劲 from selenium.webdriver.support.ui import WebDriverWait WebDriverWait类实例化时需要传入的参数:driver:WebDriver实例                      timeout:等待的最长时间                      【poll_frequency】 : 调用until或until

Actions through WebDriver will not trigger the blur event

情到浓时终转凉″ 提交于 2020-01-23 07:35:12
问题 I have a web page with two dropdowns. Selecting an option in one dropdown will update the list of options in the other dropdown through a script that's triggered by the blur event. The blur event is triggered when the focus moves away from the first dropdown. This all works fine when navigating the page manually. However, when performing the same steps through WebDriver, the blur event is never triggered, and the dropdown is thus never updated, causing my script to fail. Here's the html for

C# Selenium webdriver JavaScript errors logging

时光怂恿深爱的人放手 提交于 2020-01-23 02:45:31
问题 I'm testing with Selenium webdriver using C#. How can I log all JavaScript errors that could happen through my tests? 回答1: That depends what you mean, if you want to capture javascript errors generated in your code when you use: ((IJavaScriptExecutor)_driver).ExecuteScript("some javascript code here") Then just wrap those statements in a try/catch/finally and log the exception. If you want to capture javascript errors generated by the browser, then the short answer is: you can't easily do so.

Python Day05

给你一囗甜甜゛ 提交于 2020-01-23 02:38:38
1.requests的post请求 import requests import re headers={ 'User-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36' } response=requests.get(url='https://github.com/login',headers=headers) print(response.text) 把login页返回的cookies信息转换成字典 login_cookies=response.cookies.get_dict() authenticity_token=re.findall(' name="authenticity_token" value="(.*?)"',response.text,re.S)[0] print(authenticity_token) #拼接请求头信息 headers2={ 'Referer':'https://github.com/login', 'User-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36

Running multiple parallel Selenium WebDriver sessions

北战南征 提交于 2020-01-23 01:55:20
问题 I am wondering if we are able to run two Selenium WebDriver sessions, or how I can handle two browser windows using the same WebDriver and run them in parallel. 回答1: Try to define your TestNg suite like the below and then it will start running both at a time. <suite name="TestSuite" parallel="tests" thread-count="2"> <test name="Test1" preserve-order="true"> <parameter name="baseURL" value="http://www.amazon.com" /> <classes> <class name="package.myClass" /> </classes> </test> <test name=

爬虫实战4:用selenium爬取淘宝美食

ε祈祈猫儿з 提交于 2020-01-22 16:38:38
方案1:一次性爬取全部淘宝美食信息 1. spider.py文件如下 1 __author__ = 'Administrator' 2 from selenium import webdriver 3 from selenium.webdriver.common.by import By 4 from selenium.webdriver.support.ui import WebDriverWait 5 from selenium.webdriver.support import expected_conditions as EC 6 import re 7 from pyquery import PyQuery as pq 8 from config import * 9 import pymongo 10 11 client = pymongo.MongoClient(MONGO_URL) 12 db = client[MONGO_DB] 13 14 browser = webdriver.Chrome() 15 """ 16 如果把Chrome修改为使用PhantomJS 17 1. 首先需要安装phantomJS 18 2. 自定义一些配置参数,这里不加载图片以及使用缓存 19 browser = webdriver.PhantomJS(service_args

keep session after login - selenium - javascript

﹥>﹥吖頭↗ 提交于 2020-01-22 15:50:08
问题 I am trying to automate couple of pages using selenium web driver and node js . I was able to login , but after login I want to use same session initiated by web driver so that I can do automated testing on session protected page. This is my attempt async function login(){ Let d = await new Builder() .forBrowser('chrome') .build(); await d.get('https://demo.textdomain.com/') await d.findElement(By.id('username')).sendKeys('admin ') await d.findElement(By.id('password')).sendKeys('admin');

Python-selenium抓取动态页面 (1) -- Ubuntu下的安装

孤街醉人 提交于 2020-01-22 08:48:27
在开发网络爬虫时,我们常常需要面对动态页面。例如,网页中的JavaScript脚本会在网页加载后再修改、填充网页内容;或者需要在网页上进行交互(如登录,点击按钮、链接等’)才能获取到需要的内容。我们可以使用python-selenium库来运行JavaScript程序和模拟交互。 本文介绍在Ubuntu操作系统上安装Chrome webdriver和Python Selenium库。 1. 安装Chrome wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add - echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list apt -y install google-chrome-stable 注意看console上打印的信息,chrome的版本是79.0,接下来安装的webdriver要对应好版本。 Setting up google-chrome-stable (79.0.3945.88-1) ... update-alternatives: using /usr

selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PAT

十年热恋 提交于 2020-01-22 07:37:31
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. 原因 出现此错误的原因是未安装相应的浏览器驱动 解决办法 1、安装 webdriver 各大浏览器驱动下载地址 https://docs.seleniumhq.org/download/ Firefox: https://github.com/mozilla/geckodriver/releases/ Chrome: https://sites.google.com/a/chromium.org/chromedriver/ IE: http://selenium-release.storage.googleapis.com/index.html 2、解压、添加路径 下载解压后将对用的驱动复制到你浏览器的安装目录下, 如(C:\Program Files\Mozilla Firefox); 然后在环境变量Path中添加路径:C:\Program Files\Mozilla Firefox 再次运行即可。 或者 :每次在启动浏览器时增加 executable_path= 参数即可,如: from selenium import webdriver path = 'C:/

【WebDriver API】python之selenium操作Cookie

夙愿已清 提交于 2020-01-22 04:10:24
有时候我们需要验证浏览器中cookie是否正确,因为基于真实cookie的测试是无法通过白盒和集成测试的。WebDriver提供了操作Cookie的相关方法,可以读取,添加和删除cookie信息。 WebDriver操作cookie的方法: get_cookies():获得所有cookie信息。 get_cookie(name):返回字典的Key为“name”的cookie信息。 add_cookie(cookie_dict):添加cookie。“cookie_dict”指字典对象,必须有name和value值。 delete_cookie(name,optionsString):删除cookie信息。“name”是要删除的cookie的名称,"optionsString"是该cookie的选项,目前支持的选项包括“路径”,“域”。 delete_all_cookies():删除所有cookie信息。 下面通过get_cookies()来获取当前浏览器的cookie信息。 from selenium import webdriver driver=webdriver.Firefox() driver.get("http://www.youdao.com") #获得cookie信息 drver=webdriver.Firefox() drver.get("http://www