In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "use of the selenium library", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "the use of selenium library" bar!
1.0. Preliminary preparation
First we need to install the selenium library, pip3 install selenium
Second, we also need to install and configure ChromeDriver to interface with Selenium.
The installation and configuration is simple, you only need to download the corresponding version and copy the chromedriver.exe file to the Scripts path under the python path.
2.0. After the configuration is complete, let's give a simple example.
Webdriver.Chrome () means to create a chrome object that opens a browser on your computer
Browser.get ('https://www.baidu.com')get method means to request a page, which is opened here by Baidu
Print (browser.page_source) output to get the source code of the page, that is, the source code of Baidu page.
Browser.close () closes the browser
Other:
Browser.set_window_size (1400800) sets the size of the browser.
Browser.refresh () refresh the browser
Clear () clears the text
The obtained Baidu source code map:
3.0. After we get the page, we also need to get the node before we can continue with the following operations. Let's go on with the example.
3.1 analyze the page first
As shown in the figure, we can look at the class in the Baidu input box in developer mode. The name is wd,id, the name is wd,id, the name is kw. In the same way, we can find the class of the Baidu button is called btn self-btn bg, and the id is su.
3.2. For example, we use the class name to get the node and enter it:
Browser.find_element_by_class_name ('class IPT') uses the name of the node to get the node.
The value entered by send_keys ('selenium') is selenium.
Time.sleep (3) wait 3 seconds
Btn=browser.find_element_by_id ('su') gets a button on Baidu.
Btn.click () Click the button.
If we are proficient, we can write browser.find_element_by_class_name ('s roomipt'). Send_kes ('selenium') to get the input box and enter the content. Click the button in the same way.
There are several other ways to get nodes, let's take a look.
Find_element_by_class_name () is obtained by the name of class.
Find_element_by_id () is obtained by the name of id.
Find_element_by_name () is obtained by the property name.
Find_element_by_xpath () is obtained through xpath.
Find_element_by_tag_name () is obtained by node name. For example, input is find_element_by_tag_name ('input').
Find_element_by_css_selector () is obtained by css. If id is not kw, it is find_element_by_css_selector ('# kw').
Find_element_by_link_text () is fetched by text values, such as 123:find_element_by_link_text ('123').
Find_element_by_partial_link_text () is also taken from the text, and the meaning of partial is partial, so for example, 123:find_element_by_partial_link_text ('1') is fine.
You may find it troublesome to write like this. Of course, there is also a general method.
For example, if you use id to get, you can write: find_element (By.ID,'kw') needs to be imported
From selenium.webdriver.common.by import By
The first is the way to get, and the second is the value. The way to get it is to capitalize the letters after the original method by_. For example, if you get it by class name, it can be find_element (By.CLASS_NAME,'s_ipt').
Note: the above method only returns a single node, and if there are multiple nodes that meet the requirements, only the first node is returned.
3.3. Obtain multiple nodes:
Just change element to elements directly, and the same is true for general methods.
Example demonstration (open Baidu to enter Wuhan Scenic spot and print the title of each item):
Results:
4.0, Mouse event
4.1 example (open Baidu and hover the mouse to more products):
First, you need to import
From selenium.webdriver.common.action_chains import ActionChains
ActionChains (browser) is used to construct ActionChains objects.
Context_click (cp) hovers the mouse over an element.
Perform () commit operation, no commit will have no effect.
Other:
Move_to_element () right-click.
Double_click () double-click.
Drag_and_drop () drag.
Context_click () simulates the right-click operation and requires element positioning.
Note: I am using text to locate here, because the class name, the id name is changing.
5.0, keyboard operation
5.1. First of all, the key combination:
Send_keys (Keys.CONTROL,'a') Keys.CONTROL represents the Ctrl key, so it is ctrl+a
Send_keys (Keys.CONTROL,'c') Keys.CONTROL represents the Ctrl key, so it is ctrl+c
Everything else is the same, you know.
5.2. Other
Send_keys (Keys.BACK_SPACE) delete key (BackSpace)
Send_keys (Keys.SPACE) Spacebar (Space)
Send_keys (Keys.TAB) Tab key (Tab)
Send_keys (Keys.ESCAPE) fallback key (Esc)
Send_keys (Keys.ENTER) enter (Enter)
Get attributes, text, id, location, tag signature, size, location.
Get_attribute () gets attributes, such as class, that is, get_attribute ('class')
Size gets the element size
Text gets element text
Title gets the title of the current page
Current_url gets the url of the current page
Location gets the relative position of the element
Tag_name acquires label signature
Take Baidu as an example:
Result picture:
7.0, switch form
In Web applications, we often encounter the application of frame/iframe form nesting pages. After Selenium opens the page, the default is to find the node in frame, that is, we can not find the node of iframe. At this point, you need to:
Switch_to.frame () switches to an iframe form first. Parameters can be filled in the id and name attributes, if there is no id and name, you can also get the node through the above operation to get the node, as parameters.
8.0, window switching
Current_window_handle returns the handle to the current window
Window_handles returns all window handles
Switch_to.window () Jump window
Example:
9.0, delayed waiting (implicit waiting and explicit waiting)
Why wait? Because sometimes the page is not fully loaded, some elements may not be able to get the exception.
For example, on the Baidu page, click to log in, and then click to register immediately. If you do not delay waiting, you will report an error (you can try it yourself).
9.1 implicit wait
Implicitly_wait (10) indicates that within 10 seconds, as long as there is no node found, it will keep looking for it within 10 seconds, and an exception will be reported if it exceeds it.
9.2 explicit wait
Continue execution when a condition is established, otherwise a timeout exception is thrown when the maximum length of time is reached.
Import: from selenium.webdriver.support import expected_conditions as EC
WebDriverWait (driver, 5,0.5). Until (
EC.presence_of_element_located ((By.ID, "kw"))
)
Dirver stands for driver
5 indicates the maximum waiting time
0.5 is detected every 0.5 seconds.
The until () method is used to pass in the waiting condition
Presence_of_element_located indicates the appearance of a node.
There are other judgments to refer to the official documentation.
10. Forward and backward
Back () back
Forward () forward
11. Deal with alert and confirm generated by JavaScript
Text returns text information in alert/confirm/prompt.
Accept () accepts the existing alert box.
Dismiss () dissolves the existing alert box.
Send_keys () sends text to the warning box.
First, you need to locate the pop-up window of js.
Browser.switch_to.alert.accept () navigates to the pop-up window to perform confirmation, that is, to receive.
Browser.switch_to.alert is to locate the pop-up window.
12. Window screenshot
Get_screenshot_as_file ("E:\\ 1.jpg")
13. Operation of the drop-down box
Import: from selenium.webdriver.support.select import Select
Select ("positioning"). Select_by_value ("select value") directly navigate to the element and select it.
14. Call the js code (for example, drop down the progress bar, selenium does not provide a method)
First of all, use the setting.
J = "window.scrollTo (100450);"
The window.scrollTo () method is used to set the horizontal and vertical position of the browser window scroll bar.
The first parameter is the horizontal position and the second parameter is the vertical position.
Then use the browser.execute_script (j) method to execute the js code.
15. Operation of Cookies
Example:
Note: the above browser is all named by me, you can change it to yours.
Conclusion: I have no examples of many of the above methods, the key is to knock the code.
Thank you for your reading, the above is the content of "the use of selenium library", after the study of this article, I believe you have a deeper understanding of the use of selenium library, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.