[Python crawler] Selenium automatically access Firefox and Chrome and search for screenshots

Source: Internet
Author: User
Tags assert

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.

1  fromSeleniumImportWebdriver2  fromSelenium.webdriver.common.keysImportKeys3 ImportSYS4 5 Reload (SYS)6Sys.setdefaultencoding ('GB18030') 7Driver =Webdriver. Firefox ()8Driver.get ("http://www.baidu.com")9 assert "Baidu" inchDriver.titleTenElem = Driver.find_element_by_name ("WD") OneElem.send_keys ("Eastmount") A Elem.send_keys (Keys.return) - assert "Google" inchDriver.title -Driver.save_screenshot ('Baidu.png') the 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 33
Unicodedecodeerror: ' UTF8 ' codec can ' t decode byte 0xb0 in position 35
So this is converted to GB code, which is not highlighted here.

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

Creates an 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:
1 ImportOS2  fromSeleniumImportWebdriver3  fromSelenium.webdriver.common.keysImportKeys4 5Chromedriver ="C:\Program Files (x86) \google\chrome\application\chromedriver.exe"6os.environ["Webdriver.chrome.driver"] =Chromedriver7 8Driver =Webdriver. Chrome (Chromedriver)9Driver.get ("http://www.python.org")Ten assert "Python" inchDriver.title OneElem = Driver.find_element_by_name ("Q") AElem.send_keys ("Selenium") - Elem.send_keys (Keys.return) - assert "Google" inchDriver.title the driver.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")


Reference: Play with Python selenium:2-Getting Started 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/)

[Python crawler] Selenium automatically access Firefox and chrome for search

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.