Selenium对IE的操作

技术文档网 2021-04-20

Selenium对IE的操作

Selenium用来自动对网页进行操作,有几点好处:

1、不需要研究分析提交了什么返回了什么,有些网页提交返回的东西太多太复杂了,例如使用了DWR的网页,看起来简直头大;

2、对一些IE Only的网页,用不了Chrome的开发者工具,很难分析提交的内容和网页的结构,IE自己带的开发者工具太不好用了。

于是,对于某些较为复杂又是IE Only的网页来说,使用Selenium成了唯一选择。

Selenium操作IE需要下载IEDriverServer,最新版是3.14.0,可以从Selenium的官网找到下载地址,可惜的是那个下载地址使用了Google的短网址服务,下载不下来,其他网站有较老版本的,3.6.0版,试了一下,也是可以用的。

把IEDriverServer放在PATH里,试了一下,对于WIN7 64位系统,放在WINDOWS\SYSTEM32目录下是不行的,放在WINDOWS下可以。

使用起来跟ChromeDriver差不多,但是IE的开发者工具太弱,没有审查元素功能,有时候要找到一个元素的源代码,需要使用WebDriver的page_source,然后自己在里面找,因为IE那个开发者工具看不到脚本渲染的HTML。

用Jupyter NoteBook模拟一遍操作成功后,就可以做成一段代码自动运行了,但是由于是连续运行,中间的等待需要设置一下,使用WebDriverWait结合excepted_conditions和By,可以根据按钮是否可以点击,元素是否可见,元素里的值是否包含某个字符串等条件来等待浏览器状态达到预期,再执行下一步。

通常语句是这样的

超时则退出,从头再来,因为继续的话不知道后面会有什么奇怪的结果。

    try:
        WebDriverWait(driver,10).until(
            EC.element_to_be_clickable((By.ID,'xxxid'))
        )
    except:
        print('time out')
        driver.quit()

注意,那个10是10秒超时,也就是即使等不到,10秒后也会time out,如果遇到一些需要很长时间才能执行完毕的操作,例如查询页面,最好把这个数字弄大一点,否则很容易就抛出异常了。

根据官方文档(https://selenium-python-zh.readthedocs.io/en/latest/index.html), excepted_conditions有以下几种:

7.14. Expected conditions Support

class selenium.webdriver.support.expected_conditions.alert_is_present

Bases: object

Expect an alert to be present.

class selenium.webdriver.support.expected_conditions.element_located_selection_state_to_be(locator, is_selected)

Bases: object

An expectation to locate an element and check if the selection state specified is in that state. locator is a tuple of (by, path) is_selected is a boolean

class selenium.webdriver.support.expected_conditions.element_located_to_be_selected(locator)

Bases: object

An expectation for the element to be located is selected. locator is a tuple of (by, path)

class selenium.webdriver.support.expected_conditions.element_selection_state_to_be(element, is_selected)

Bases: object

An expectation for checking if the given element is selected. element is WebElement object is_selected is a Boolean.”

class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)

Bases: object

An Expectation for checking an element is visible and enabled such that you can click it.

class selenium.webdriver.support.expected_conditions.element_to_be_selected(element)

Bases: object

An expectation for checking the selection is selected. element is WebElement object

class selenium.webdriver.support.expected_conditions.frame_to_be_available_and_switch_to_it(locator)

Bases: object

An expectation for checking whether the given frame is available to switch to. If the frame is available it switches the given driver to the specified frame.

class selenium.webdriver.support.expected_conditions.invisibility_of_element_located(locator)

Bases: object

An Expectation for checking that an element is either invisible or not present on the DOM.

locator used to find the element

class selenium.webdriver.support.expected_conditions.new_window_is_opened(current_handles)

Bases: object

An expectation that a new window will be opened and have the number of windows handles increase

class selenium.webdriver.support.expected_conditions.number_of_windows_to_be(num_windows)

Bases: object

An expectation for the number of windows to be a certain value.

class selenium.webdriver.support.expected_conditions.presence_of_all_elements_located(locator)

Bases: object

An expectation for checking that there is at least one element present on a web page. locator is used to find the element returns the list of WebElements once they are located

class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)

Bases: object

An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible. locator - used to find the element returns the WebElement once it is located

class selenium.webdriver.support.expected_conditions.staleness_of(element)

Bases: object

Wait until an element is no longer attached to the DOM. element is the element to wait for. returns False if the element is still attached to the DOM, true otherwise.

class selenium.webdriver.support.expected_conditions.text_to_be_present_in_element(locator, text_)

Bases: object

An expectation for checking if the given text is present in the specified element. locator, text

class selenium.webdriver.support.expected_conditions.text_to_be_present_in_element_value(locator, text_)

Bases: object

An expectation for checking if the given text is present in the element’s locator, text

class selenium.webdriver.support.expected_conditions.title_contains(title)

Bases: object

An expectation for checking that the title contains a case-sensitive substring. title is the fragment of title expected returns True when the title matches, False otherwise

class selenium.webdriver.support.expected_conditions.title_is(title)

Bases: object

An expectation for checking the title of a page. title is the expected title, which must be an exact match returns True if the title matches, false otherwise.

class selenium.webdriver.support.expected_conditions.url_changes(url)

Bases: object

An expectation for checking the current url. url is the expected url, which must not be an exact match returns True if the url is different, false otherwise.

class selenium.webdriver.support.expected_conditions.url_contains(url)

Bases: object

An expectation for checking that the current url contains a case-sensitive substring. url is the fragment of url expected, returns True when the title matches, False otherwise

class selenium.webdriver.support.expected_conditions.url_matches(pattern)

Bases: object

An expectation for checking the current url. pattern is the expected pattern, which must be an exact match returns True if the title matches, false otherwise.

class selenium.webdriver.support.expected_conditions.url_to_be(url)

Bases: object

An expectation for checking the current url. url is the expected url, which must be an exact match returns True if the title matches, false otherwise.

class selenium.webdriver.support.expected_conditions.visibility_of(element)

Bases: object

An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. element is the WebElement returns the (same) WebElement once it is visible

class selenium.webdriver.support.expected_conditions.visibility_of_all_elements_located(locator)

Bases: object

An expectation for checking that all elements are present on the DOM of a page and visible. Visibility means that the elements are not only displayed but also has a height and width that is greater than 0. locator - used to find the elements returns the list of WebElements once they are located and visible

class selenium.webdriver.support.expected_conditions.visibility_of_any_elements_located(locator)

Bases: object

An expectation for checking that there is at least one element visible on a web page. locator is used to find the element returns the list of WebElements once they are located

class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)

Bases: object

An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. locator - used to find the element returns the WebElement once it is located and visible

By则有以下几种:

7.5. Locate elements By

class selenium.webdriver.common.by.By

Bases: object

Set of supported locator strategies.

CLASS_NAME = 'class name'

CSS_SELECTOR = 'css selector'

ID = 'id'

LINK_TEXT = 'link text'

NAME = 'name'

PARTIAL_LINK_TEXT = 'partial link text'

TAG_NAME = 'tag name'

XPATH = 'xpath'

IE还有一个坑,是不能通过selenium来操作下载过程,因为IE的下载每次都会弹出一个窗口提示你保存还是打开,无法自动控制,不过如果知道了URL,可以通过requests等其他手段下载。Firefox和Chrome则没有这个问题,可以配置浏览器本身来达到自动下载到某个指定位置的目的。

相关文章

  1. 硅谷互联网公司的开发流程

    开发流程包括这么几个阶段: OKR 的设立; 主项目及其子项目的确立; 每个子项目的生命周期; 主项目的生命周期; 收尾、维护、复盘。 第一点,OKR 的设立 所有项目的起始,都应该从 Ro

  2. RESTful-表述性状态转移风格

    REST英文全拼:Representational State Transfer 面向资源编程 资源指的就是一类数据 产品表->就是产品资源 最重要的是如何表示一个资源 地址即

  3. 稳定性思考

    产品功能线 0-1: 当系统从无到有的时候,首要考虑的是研发效率,功能快速迭代,满足快速增长的业务需求 1-10 系统已经搭建起来,此时考虑的是系统的稳定性。 可用性:1.隔离:区分出核心和非核心功能

  4. Supervisor守护队列发邮件

    安装 CentOS: yum -y install supervisor Debien/Ubuntu适用:apt-get install supervisor 配置 修改主配置文件:vim /et

  5. 安装libsodium,让服务器支持chacha20等加密方式

    用chacha20加密方式需要安装libsodium 注意:libsodium从1.0.15开始就废弃了aes-128-ctr yum install wget m2crypto git libsod

随机推荐

  1. 硅谷互联网公司的开发流程

    开发流程包括这么几个阶段: OKR 的设立; 主项目及其子项目的确立; 每个子项目的生命周期; 主项目的生命周期; 收尾、维护、复盘。 第一点,OKR 的设立 所有项目的起始,都应该从 Ro

  2. RESTful-表述性状态转移风格

    REST英文全拼:Representational State Transfer 面向资源编程 资源指的就是一类数据 产品表->就是产品资源 最重要的是如何表示一个资源 地址即

  3. 稳定性思考

    产品功能线 0-1: 当系统从无到有的时候,首要考虑的是研发效率,功能快速迭代,满足快速增长的业务需求 1-10 系统已经搭建起来,此时考虑的是系统的稳定性。 可用性:1.隔离:区分出核心和非核心功能

  4. Supervisor守护队列发邮件

    安装 CentOS: yum -y install supervisor Debien/Ubuntu适用:apt-get install supervisor 配置 修改主配置文件:vim /et

  5. 安装libsodium,让服务器支持chacha20等加密方式

    用chacha20加密方式需要安装libsodium 注意:libsodium从1.0.15开始就废弃了aes-128-ctr yum install wget m2crypto git libsod