First, the basic operation
After running the code the following code, you will find below the Chrome address bar, it is under the control of automated testing software
from the Selenium Import webdriver # Tour Launch Google Driver = webdriver.Chorm () # open the Baidu page # driver.get (url)
driver.get ( " www.baidu.com " )
# Set the page size setting is the first parameter value long browser, and the second parameter value is set by the browser width:
driver.set_window_size (1200.1500)
# page maximize
driver.maximize_window (
Other basic commands:
# Forward driver.back () # Back driver.forward () # refresh driver.refresh () # exit driver.quit ()
Second, the positioning elements:
a total of eight ways
find_element_by_id()
find_element_by_name()
find_element_by_class_name()
find_element_by_tag_name()
find_element_by_link_text()
find_element_by_partial_link_text()
find_element_by_xpath()
find_element_by_css_selector()
1, the positioning ID
ID HTML attribute is very important, HTML elements with a specific ID value can be CSS, and JavaScript script style selectors to find and perform certain functions. In order to facilitate the distinction between general HTML page we will not HTML elements disposed the same ID.
Unique ID selector, although repeated CSS style corresponding to the ID will take effect, if the corresponding element in JavaScript to acquire the ID, an error because it can take a ID.
In Baidu search as an example, how to find the ID program. This is the search box Baidu code: <input type = "text" class = "s_ipt" name = "wd" id = "kw" maxlength = "100" autocomplete = "off"> focus here, ID = "kw "by here know, Baidu search box ID for kw, you can find the ID. Search then get confirmation button ID: <input type = "submit" value = "Baidu," id = "su" class = "btn self-btn bg s_btn"> can be seen by the above code, ID = "su "
# Coding: UTF. 8- # import library from Selenium Import the webdriver Import Time Improt Time # open a browser and opening Baidu Driver = webdriver.Chrome () driver.get ( " https://www.baIDu.com/ " ) # by ID lookup, then enter and click driver.find_element_by_id ( " kw " ) .send_keys ( " the Selenium " ) driver.find_element_by_id ( " su " ) .click () # wait 3 seconds the time.sleep () # exit the browser driver. quit ()
2. The name anchor
Similar to the id
The positioning Xpath
Open the Developer Tools browser, and then navigate to the search box, this time, we are below the developer tools should have been positioned on the corresponding code, we selected this line of code (if the code box your developer tools are cover, then you can slide up and down to the appropriate exhibition
opened, or turn off the pop-up Console and What's New), then right click and select copy, this time in the secondary pop-up selection box, click on the lowermost Copy XPath, we can get to the machine-generated
. . . Other similar will not repeat them