In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
1. Several methods of locating elements
Find_element_by_id (priority, commonly used), corresponding to the id attribute
Find_element_by_name (priority, commonly used), corresponding to the name attribute
Find_element_by_class_name, which corresponds to the value of the class attribute
Find_element_by_tag_name, which corresponds to the tag name, such as input,p,span,title,body....
Find_element_by_link_text, which corresponds to the hyperlinked text content
Find_element_by_partial_link_text, which corresponds to the text content of some hyperlinks
Find_element_by_xpath, which corresponds to the path of the element, which can be obtained through the firebug tool
Find_element_by_css_selector, located through the css attribute, class uses. Id uses #
Example:
Driver.find_element_by_css_selector (".s _ ipt") .send_keys ("selenium")
Driver.find_element_by_css_selector ("# su") .click ()
2.By positioning
Find_element (By.ID, "kw")
Find_element (By.NAME, "wd")
Find_element (By.CLASS_NAME, "s_ipt")
Find_element (By.TAG_NAME, "input")
Find_element (By.LINK_TEXT,u News)
Find_element (By.PARTIAL_LINK_TEXT,u "new")
Find_element (By.XPATH, "/ / * [@ class='bg slicbtn']")
Find_element (By.CSS_SELECTOR, "span.bg s_btn_wr > input#su")
3. Several browser operations
Description: to operate the browser, you need to put the browser plug-in directory into the environment variable, otherwise you can't find it.
From selenium import webdriver
Driver = webdriver.Ie ()-- IE browser, plug-in named IEDriverServer.exe
Driver = webdriver.Firefox ()-- Firefox browser, plug-in named geckodriver.exe
Driver = webdriver.Chrome ()-Google browser, plug-in named chromedriver.exe
Driver = webdriver.Edge ()-Edge browser (included with windows10 system) without plug-ins
Operate 360 browser:
The browser kernel is based on chrome browser, so the plug-in also uses chromedriver.exe, but you have to configure it.
Chrome_options = webdriver.ChromeOptions ()
Chrome_options.binary_location = r "C:\ Users\ kevin\ AppData\ Roaming\ 360se6\ Application\ 360se.exe" # this is the path to the 360 secure browser
Chrome_options.add_argument # add some startup parameters here
D = webdriver.Chrome (chrome_options=chrome_options)
4. Control browser
Set width and height: driver.set_window_size (400500),-set width 400, height 500
Set full screen: driver.maximize_windows ()
Forward: driver.forward ()
Back: driver.back ()
Refresh the current page: driver.refresh ()
5. Element operation to which you navigate
Driver.find_element_by_xx ('xx') .click ()-Click action
Driver.find_element_by_xx ('xx') .clear ()-- clear operation, clear the contents of the text box
Driver.find_element_by_xx ('xx') .send_keys (' 123')-enter data into the text box
Driver.find_element_by_xx ('xx') .submit ()-- submit operation, which is equivalent to enter. If you enter the content in the search box, execute this method to directly perform the search action.
Driver.find_element_by_xx ('xx') .size ()-- gets the size of the element
Driver.find_element_by_xx ('xx') .text-- gets the text of the element
Driver.find_element_by_xx ('xx'). Get_attribute (' id')-- gets the id attribute, which can be obtained by this method, such as name,id,type,class.
Driver.find_element_by_xx ('xx') .is_displayed ()-gets whether the element is visible, returns True if visible, otherwise False
6. Mouse event
Common methods for mouse operation are provided in the Actions class
Perform ()-- executes the behavior stored in all Actions classes
Context_click ()-right click
Double_click ()-double click
Drap_and_drop ()-drag
Move_to_element-hover
Call example:
Context_click ()
From selenium.webdriver.common.action_chains ActionChainsdriver = webdriver.Ie () right_click = driver.find_element_by_id () ActionChains (driver). Context_click (right_click). Perform ()
Other method calls are the same as above, but just change the method
7. Keyboard operation
Analog key combination CTRL+A,CTRL+C, etc.
From selenium.webdriver.common.keys import Keys
Backspace key Keys.BACK_SPACE-driver.find_elemennt_by_id (') .send_keys (Keys.BACK_SPACE)
Space bar Keys.SPACE-driver.find_elemennt_by_id ('') .send_keys (Keys.SPACE)
Enter CTRL+A-driver.find_elemennt_by_id ('') .send_keys (Keys.CONTROL,'a')
Enter key-driver.find_elemennt_by_id ('') .send_keys (Keys.ENTER)
8. Set element wait
Explicitly wait:
Always detect and wait for an element to appear or disappear. If the maximum length condition has not been established, a timeout exception is thrown.
From selenium import webdriver
From selenium.webdriver.support.ui import WebDriverWait
From selenium.webdriver.support import expected_conditions as EC
Driver = webdriver.Ie ()
Driver.get ("https://www.baidu.com")"
Element = WebDriverWait (driver,5,0.5). Until (
EC.presence_of_ element_located ((By.id, "kw"))
)
Element.send_keys ('hello')
5: indicates the maximum waiting time
0.5: detection interval, that is, how often is the test (in seconds)
Until: there is also a until not until the condition in parentheses is established
Implicit wait:
Driver.implicitly_wait (10), in seconds, is not set to 0 by default. It is generally written at the beginning of the use case and is valid for the entire use case. When locating an element, if the element is not loaded, it cannot be located. This method will always detect within the set time, and proceed down as soon as it is detected, otherwise a timeout exception will be thrown.
Sleep hibernation:
Forced to wait for a period of time
Import time
Time.sleep (10)
9. Locate a set of elements
Find_elements_by_xx, just like locating a single element, but element is elements
Example: check all checkbox
Checkboxs = driver.find_elements_by_tag_name ("input")
For checkbox in checkboxs:
If checkbox.get_attribute ("type") = = "checkbox":
Checkbox.click ()
10. Operation drop-down selection box
Methods:
Select:
Select_by_index ()-through the index
Select_by_value ()-- through the value value
Select_by_visiable_text ()-- through the displayed text
Reverse election:
Deselect_by_index ()-through the index
Deselect_by_value ()-- through the value value
Deselect_by_visiable_text ()-- through the displayed text
Deselect_all ()-deselect all
Example:
Show text 1
Show text 2
Show text 3
Select an example:
Select_by_index (1)-through the index
Select_by_value ('o2')-- through the value value
Select_by_visiable_text ('display text 2')-- through the displayed text
Complete example:
From selenium.webdriver.support.ui import Select
Select = Select (driver.find_element_by_id ("sid"))
Select.select_by_index (1)
11. Multi-form switching
Many pages have form nesting, so it is impossible to navigate directly to elements in other forms from one form, so you need to switch forms.
F = driver.find_element_by_id ("fr")-navigate to the form where id is fr. The positioning method is universal.
Driver.switch_to.frame (f)-switch to the form where id is fr
Jump to the outermost form when the operation is complete: driver.switch_to.default_content ()
twelve。 Multi-window switching
Get the handle of the current window: current_handle = driver.current_window_handle
Get all windows handle: handles = driver.window_handles
Switch to another window
For hand in handles:
If hand! = current_handle:
Driver.swith_to.window (hand)-Toggle window
13. Pop-up window processing
For the pop-up window of alert,prompt,confirm, you can use driver.switch_to_alert () to capture and deal with it.
Methods:
Text-returns the text message of the pop-up window
Accept ()-- accept the pop-up window, that is, click OK
Dismiss ()-cancel the pop-up window
Call example:
Driver.switch_to_alert () .accept ()-Click OK
14. Upload files
Method 1: directly enter the file path into the text box through the send_keys () method, and click the submit button.
Method 2: package the operation process into an exe file through the AutoIt tool, and then call it through python
15. Manipulate cookie
Get all cookie: driver.get_cookies ()
Get the cookie: driver.get_cookie ('xx') named xx
Add cookie: driver.add_cookie (dict)-the parameter is a dictionary object
Delete all cookie: driver.delete_all_cookies ()
16. Call JavaScript
Js = "window.scrollTo (100450);"
Driver.execute_script (js)
17. Deal with HTM5 video playback
Example:
Video = driver.find_element_by_xpath ("body/Section [1] / div/video")
Driver.execute_script ("return arguments [0] .play ()", video)-play
Driver.execute_script ("return arguments [0] .pause ()", video)-pause
18. Window screenshot
Driver.get_screenshot_as_file ("D:\ xxx\ xx.png")
19. Close the window
Driver.close ()-close the current window
Driver.quit ()-close all windows
20. CAPTCHA processing
1. Remove the CAPTCHA
two。 Set universal verification code
3. Use cookie
4. CAPTCHA recognition
5. The CAPTCHA is stored in cookie
21. To manipulate a browser that is already open, take chrome as an example
1. Start the browser manually, in debug mode
C:\ Program Files (x86) > chrome.exe-- remote-debugging-port=9222
two。 Add selenium configuration
Options = webdriver.ChromeOptions ()
Options.debugger_address (options)
Driver = webdriver.Chrome (options)
The rest will operate as normal as before.
Problems encountered and solutions:
1. The following error is reported
Selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value'
(Session info: chrome=75.0.3770.142)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f), platform=Windows NT 10.0.17134 x861664)
Solution:
This is due to a mismatch between the versions of chromedriver and chrome browsers, with the following matching information:
Chromedriver supported Chrome version v2.41v67-69v2.40v66-68v2.39v66-68v2.38v65-67v2.37v64-66v2.36v63-65v2.35v62-64v2.34v61-63v2.33v60-62v2.32v59-61v2.31v58-60v2.30v58-60v2.29v56-58v2.28v55-57v2.27v54-56v2.26v53-55v2.25v53-55v2.24v52-54v2.23v51-53v2.22v49-52v2.21v46-50v2.20v43-48v2.19v43-47v2.18v43-46v2.17v42-43v2.13v42-45v2.15v40-43v2.14v39 -42v2.13v38-41v2.12v36-40v2.11v36-40v2.10v33-36v2.9v31-34v2.8v30-33v2.7v30-33v2.6v29-32v2.5v29-32v2.4v29-32
Download address: http://npm.taobao.org/mirrors/chromedriver/
Http://chromedriver.storage.googleapis.com/index.html
If the chrome version is too high, it is recommended to use an IE browser instead
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.