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