[Python crawler] Windows selenium automatically accesses Firefox and Chrome and implements search screenshots

Source: Internet
Author: User

The first two articles introduced the installation, this article is considered a simple advanced application bar! It is a feature that automatically accesses Firefox and Chrome and implements search under Windows via Selenium+python.
[python crawler] install PHANTOMJS and Casperjs in Windows and introduction (top)
[python crawler] installs pip+phantomjs+selenium under Windows

Automatically Access Firefox

The selenium environment can be installed with reference to the previous article, and currently selenium the tools supported for Web application testing include IE, Mozilla Firefox, Mozilla Suite, Chrome, and more. But since Firefox is the default installation path, Webdriver can access it normally, and chrome and IE need to set the driver path.

From selenium import webdriverfrom selenium.webdriver.common.keys import keysimport sys reload (SYS) Sys.setdefaultencoding (' gb18030 ') driver = Webdriver. Firefox () driver.get ("http://www.baidu.com") assert "Baidu" in Driver.titleelem = Driver.find_element_by_name ("WD") Elem.send_keys ("Eastmount") Elem.send_keys (Keys.return) assert "Google" in Driver.titledriver.save_screenshot (' Baidu.png ') Driver.close () Driver.quit ()
Run the effect as shown, automatically call the Firefox browser search, and output an assertion error:
assert "Google" in Driver.title assertionerror


Source Analysis
Official Document Address:Http://selenium-python.readthedocs.org/getting-started.html

from Selenium import webdriver
       from Selenium.webdriver.common.keys Import keys
       import sys

        First, import the Selenium.webdriver template, which provides a way to implement Webdriver, which currently supports Firefox, Chrome, ie, and remote. The keys class is also imported, which provides keyboard shortcuts, such as Reture, F1, Alt, and so on. The last import of SYS is primarily the setup encoding.

reload (SYS)
        Sys.setdefaultencoding (' gb18030 ')  

        errors may be encountered in Chinese:
        unicodedecodeerror: ' ASCII ' codec can ' t decode byte 0xc4 in position
        unicodedecodeerror: ' UTF8 ' codec can ' t decode byte 0xb0 in position
        So this is converted to G B-coding, the article does not focus on the introduction.

driver = Webdriver. Firefox ()
       driver.get ("http://www.baidu.com")

        Create firefoxwebdriver instance. Firefox is the simplest, and other chrome needs driver and configuration paths. Next through Driver.get () to open the Baidu URL page, Webdriver will wait for the page elements to be loaded before the control back to the script. However, if you are opening a page that contains many ajax,webdriver during the loading process, it may not be possible to accurately determine when the page is loaded.

assert" Baidu "in Driver.title
       assert" Google "in Driver.title

Next use the assertion to determine whether the title of the article contains "Baidu" and "Google". The corresponding title is "Baidu a bit, you know", so that "Baidu" includes, and "Google" will appear assertion error.
The assertion can also be used in order to determine whether the result returns successfully, as well as submitting the page for the return result.

Elem = Driver.find_element_by_name ("WD")

Webdriver provides a number of ways, such as find_element_by_*, to match the elements to find. If you use the Name property to find method Find_element_by_name to locate the input box, review the element name=wd.
element Positioning method can refer to the official website:Locating Elements


elem.send_keys (" Eastmount ")

elem.send_keys (Keys.return)

The Send_keys method can be used to simulate a keyboard operation, which is equivalent to entering "Eastmount" in the search box and then pressing ENTER to search. but first, you import the keys class from Selenium.webdriver.common.keys.

Driver.save_screenshot (' Baidu.png ')

Driver.close ()

Driver.quit ()

Finally, call Save_screenshot, but the picture is in the process, how to get the last loaded picture? At the same time, the operation completes and closes the browser. Of course, you can also call the Quit () method, the difference is that the Quit () method exits the browser, and the close () method simply closes the page, but if only one page is open, the close () method also exits the browser.


automatic access to Chrome
First download the chromedriver and put it in the Chrome installation directory. Errors may be encountered:
Webdriverexception:message: ' chromedriver ' executable needs to being in PATH. Refer to the official website solution: How to use Chromedriver, I am using the settings driver Environment.
The code is as follows:
Import osfrom Selenium import webdriverfrom selenium.webdriver.common.keys import keyschromedriver = "C:\Program Files (x \google\chrome\application\chromedriver.exe "os.environ[" webdriver.chrome.driver "] = Chromedriverdriver = Webdriver. Chrome (Chromedriver) driver.get ("http://www.python.org") assert "Python" in Driver.titleelem = driver.find_element_by _name ("q") Elem.send_keys ("Selenium") Elem.send_keys (Keys.return) assert "Google" in Driver.titledriver.close () Driver.quit ()
You need to place Chromedriver as follows, and can be set by code. but because my chrome may be bug-fixed all the time, the error is always turned on.

Driver = Webdriver. Chrome (executable_path= "G:\chromedriver.exe")



References:Play with Python selenium:2-introduction example and Analysis-Reiki
Build python+selenium2 Automated test environment < Two >:ie, Chrome and Firefox run
Using selenium to realize the crawl of a micro-blog search data
Robotframework+seleniumlibrary Web Automation Test (iii)

finally hope that the basic article to help you! If there are shortcomings, please Haihan ~
(By:eastmount 2015-8-20 4 o'clock in the afternoon http://blog.csdn.net/eastmount/)


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[python crawler] Windows selenium automatically access Firefox and Chrome and implement search

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.