Python:Selenium和PhantomJS(二)

简介: Python:Selenium和PhantomJS(二)

鼠标动作链



有些时候,我们需要再页面上模拟一些鼠标操作,比如双击、右击、拖拽甚至按住不动等,我们可以通过导入 ActionChains 类来做到:


示例:


#导入 ActionChains 类
from selenium.webdriver import ActionChains
# 鼠标移动到 ac 位置
ac = driver.find_element_by_xpath('element')
ActionChains(driver).move_to_element(ac).perform()
# 在 ac 位置单击
ac = driver.find_element_by_xpath("elementA")
ActionChains(driver).move_to_element(ac).click(ac).perform()
# 在 ac 位置双击
ac = driver.find_element_by_xpath("elementB")
ActionChains(driver).move_to_element(ac).double_click(ac).perform()
# 在 ac 位置右击
ac = driver.find_element_by_xpath("elementC")
ActionChains(driver).move_to_element(ac).context_click(ac).perform()
# 在 ac 位置左键单击hold住
ac = driver.find_element_by_xpath('elementF')
ActionChains(driver).move_to_element(ac).click_and_hold(ac).perform()
# 将 ac1 拖拽到 ac2 位置
ac1 = driver.find_element_by_xpath('elementD')
ac2 = driver.find_element_by_xpath('elementE')
ActionChains(driver).drag_and_drop(ac1, ac2).perform()


填充表单



我们已经知道了怎样向文本框中输入文字,但是有时候我们会碰到<select> </select>标签的下拉框。直接点击下拉框中的选项不一定可行。


<select id="status" class="form-control valid" onchange="" name="status">
    <option value=""></option>
    <option value="0">未审核</option>
    <option value="1">初审通过</option>
    <option value="2">复审通过</option>
    <option value="3">审核不通过</option>
</select>

image.png


Selenium专门提供了Select类来处理下拉框。 其实 WebDriver 中提供了一个叫 Select 的方法,可以帮助我们完成这些事情:

# 导入 Select 类
from selenium.webdriver.support.ui import Select
# 找到 name 的选项卡
select = Select(driver.find_element_by_name('status'))
# 
select.select_by_index(1)
select.select_by_value("0")
select.select_by_visible_text(u"未审核")

以上是三种选择下拉框的方式,它可以根据索引来选择,可以根据值来选择,可以根据文字来选择。注意:


  • index 索引从 0 开始
  • value是option标签的一个属性值,并不是显示在下拉框中的值
  • visible_text是在option标签文本的值,是显示在下拉框的值


全部取消选择怎么办呢?很简单:


select.deselect_all()


弹窗处理



当你触发了某个事件之后,页面出现了弹窗提示,处理这个提示或者获取提示信息方法如下:

alert = driver.switch_to_alert()


页面切换



一个浏览器肯定会有很多窗口,所以我们肯定要有方法来实现窗口的切换。切换窗口的方法如下:

driver.switch_to.window("this is window name")

也可以使用 window_handles 方法来获取每个窗口的操作对象。例如:


for handle in driver.window_handles:
    driver.switch_to_window(handle)


页面前进和后退



操作页面的前进和后退功能:


driver.forward()     #前进
driver.back()        # 后退


Cookies



获取页面每个Cookies值,用法如下


for cookie in driver.get_cookies():
    print "%s -> %s" % (cookie['name'], cookie['value'])

删除Cookies,用法如下


# By name
driver.delete_cookie("CookieName")
# all
driver.delete_all_cookies()


页面等待



注意:这是非常重要的一部分!!


现在的网页越来越多采用了 Ajax 技术,这样程序便不能确定何时某个元素完全加载出来了。如果实际页面等待时间过长导致某个dom元素还没出来,但是你的代码直接使用了这个WebElement,那么就会抛出NullPointer的异常。


为了避免这种元素定位困难而且会提高产生 ElementNotVisibleException 的概率。所以 Selenium 提供了两种等待方式,一种是隐式等待,一种是显式等待。


隐式等待是等待特定的时间,显式等待是指定某一条件直到这个条件成立时继续执行。


显式等待


显式等待指定某个条件,然后设置最长等待时间。如果在这个时间还没有找到元素,那么便会抛出异常了。


from selenium import webdriver
from selenium.webdriver.common.by import By
# WebDriverWait 库,负责循环等待
from selenium.webdriver.support.ui import WebDriverWait
# expected_conditions 类,负责条件出发
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("http://www.xxxxx.com/loading")
try:
    # 页面一直循环,直到 id="myDynamicElement" 出现
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()


如果不写参数,程序默认会 0.5s 调用一次来查看元素是否已经生成,如果本来元素就是存在的,那么会立即返回。


下面是一些内置的等待条件,你可以直接调用这些条件,而不用自己写某些等待条件了。


title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
text_to_be_present_in_element
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
invisibility_of_element_located
element_to_be_clickable – it is Displayed and Enabled.
staleness_of
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present


隐式等待


隐式等待比较简单,就是简单地设置一个等待时间,单位为秒。


from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10) # seconds
driver.get("http://www.xxxxx.com/loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")


当然如果不设置,默认等待时间为0。


目录
相关文章
|
1月前
|
Web App开发 数据采集 Java
【Python】已完美解决:selenium.common.exceptions.SessionNotCreatedException: Message: session not created
【Python】已完美解决:selenium.common.exceptions.SessionNotCreatedException: Message: session not created
124 0
|
8天前
|
Web App开发 数据可视化 Python
Python Selenium获取boss直聘招聘信息
Python Selenium获取boss直聘招聘信息
24 5
Python Selenium获取boss直聘招聘信息
|
7天前
|
数据采集 Python
如何用Python Selenium和WebDriver抓取LinkedIn数据并保存登录状态
本文介绍了使用Python Selenium和WebDriver库抓取LinkedIn数据的方法。首先,安装Selenium库和对应的WebDriver,然后配置爬虫代理IP以避免频繁请求被检测。接下来,设置user-agent和cookies以模拟真实用户行为,实现登录并保持状态。登录后,使用WebDriver抓取目标页面数据,如用户名、年龄、性别和简历信息。最后,强调了优化代码、处理异常和遵守使用条款的重要性,以提高效率并避免账号被封禁。
如何用Python Selenium和WebDriver抓取LinkedIn数据并保存登录状态
|
5天前
|
Web App开发 数据采集 测试技术
五分钟轻松掌握 Python 自动化测试 Selenium
本文主要介绍了 Selenium 相关内容,主要涉及 Selenium 知识面,从开始的 Python 小案例,到后面的 API 全面了解,以及 Selenium 的常用功能,到最后的 XPATH 以及爬虫的认知。这些内容已经能够全面,且具有实践性。
|
20天前
|
测试技术 数据安全/隐私保护 Python
大麦网抢票攻略:使用Python Selenium实现
大麦网抢票攻略:使用Python Selenium实现
|
13天前
|
数据采集 Web App开发 存储
基于Python的51job(前程无忧)招聘网站数据采集,通过selenium绕过网站反爬,可以采集全国各地数十万条招聘信息
本文介绍了一个使用Python和Selenium库实现的51job(前程无忧)招聘网站数据采集工具,该工具能够绕过网站的反爬机制,自动化登录、搜索并采集全国各地的招聘信息,将数据保存至CSV文件中。
|
1月前
|
Web App开发 测试技术 Python
【Python】已解决:selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrom
【Python】已解决:selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrom
87 6
|
2月前
|
数据采集 Web App开发 数据处理
一步步教你用Python Selenium抓取动态网页任意行数据
使用Python Selenium爬取动态网页,结合代理IP提升抓取效率。安装Selenium,配置代理(如亿牛云),设置User-Agent和Cookies以模拟用户行为。示例代码展示如何使用XPath提取表格数据,处理异常,并通过隐式等待确保页面加载完成。代理、模拟浏览器行为和正确配置增强爬虫性能和成功率。
一步步教你用Python Selenium抓取动态网页任意行数据
|
20天前
|
Web App开发 JavaScript 前端开发
自动化测试的新篇章:使用Selenium和Python进行Web应用测试
【7月更文挑战第31天】在软件开发生命周期中,测试环节是确保产品质量的关键步骤。随着技术的发展,自动化测试已成为提升效率、减少人为错误的重要手段。本文将通过实例介绍如何结合Selenium WebDriver和Python语言,构建一个基本的自动化测试框架,旨在帮助读者理解并实现自动化测试脚本,从而提高软件测试的效率和质量。
23 0
|
2月前
|
XML 测试技术 数据格式
软件测试之 自动化测试 基于Python语言使用Selenium、ddt、unitTest 实现自动化测试(下)
软件测试之 自动化测试 基于Python语言使用Selenium、ddt、unitTest 实现自动化测试(下)
41 3