[Selenium] Start with an instance, access the Python website and search for keywords

Source: Internet
Author: User

The following is a simple example

Import Time fromSeleniumImportWebdriver fromSelenium.webdriver.common.keysImportKeysbrowser=Webdriver. Chrome () Browser.get ("http://www.python.org")assert "Python" inchBrowser.titleelem= Browser.find_element_by_name ("Q") Elem.send_keys ("Pycon") Elem.send_keys (Keys.return)assert "No results found."  not inchBrowser.page_sourcetime.sleep (20) Browser.close ()
Analyze the instance

The Selenium.webdriver module provides a webdriver implementation that can now support Firefox, Chrome, IE and remote.

The keys module provides some keyboard operations, such as Return, F1, ALT, etc.

From selenium import Webdriver
From Selenium.webdriver.common.keys import keys

Next, create a browser instance of Chrome

Browser = Webdriver. Chrome ()

If you create IE, you can use the following methods

Browser = Webdriver. Ie ()

When you run for the first time, you may be prompted with an error

Selenium.common.exceptions.WebDriverException:Message:IEDriver executable needs to being available in the path.

is because executing these instances requires adding the appropriate driver

The URL is then accessed through the Get function. What you need to declare here is that the Get function waits for the page to load safely before the control is handed back to the Python script.

When the page is loaded, determine if the title of the page contains "Python"

Browser.get (http://www.python.org)
Assert "Python" in Browser.title

Then search for the keyword Pycon in the search box.

1. Find the search box first

Elem = Browser.find_element_by_name ("q")

2. Send keywords like the search box

Elem.send_keys ("Pycon")

3. Simulate keyboard enter to submit search requirements

Elem.send_keys (Keys.return)

After the search request is submitted, determine if there is any content found

Assert "No results found." Not in Browser.page_source

Close the browser after 20 seconds of last wait

Browser.close ()

[Selenium] starts with an instance, accesses the Python web site and searches for keywords

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.