程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python+selenium common APIs

編輯:Python

Python+selenium Commonly used API


Just started before selenium I have also summed up a little in automation . Every few months , Pick up again , deep feeling , Confucius said :“ Consider the past you shall know the future ”. Cough ~ Good good , Get to the point .

Here I would like to summarize the problems encountered .

demand : There is such a CSV file , Need to analyze CSV Something in , Then automatically query from a website , Then judge whether the query result has .

csv The analysis of is not recorded here , Mainly record the main knowledge used .

The first, of course, is the positioning of elements ,selenium Automation has to turn if it wants to play , This is the basic skill .

One 、 Element localization


webdriver.find_element_by_id() # Elements id Attribute positioning 
webdriver.find_element_by_css_selector() #css Selector positioning 
webdriver.find_element_by_xpath() # xpath location 
...

Please refer to this blog for more information https://blog.csdn.net/liu_xzhen/article/details/88885362, The introduction is very detailed .
In my opinion, the best use should be find_element_by_css_selector This way, , But this way you need to be familiar with CSS Syntax for selectors . After playing, the little rooster will be the one who spots him , It's easy to use. Don't do it .

What impressed me most about this task was the property selector , I don't know if the official call it that , But I think it fits . Sometimes an action on a web page just dynamically appends an element's attribute , We can deal with it this way .

 example :input[name='kw'] # location input Element of type and name Attribute value bit ‘kw’ The elements of 

The most common problem encountered after the element positioning problem is the timeout problem .

Two 、 Timeout problem

The speed of web page loading is closely related to the network speed . As the network fluctuates , We can't control the waiting time accurately . I was just waiting for you , One second doesn't work. Set two seconds … This makes me look stupid . So this is also a great gain of my mission .

There are three main ways to deal with timeout problems :

1、time Modular sheep function , Follow java Threads of are similar to , The program pauses for a specified time before resuming execution , Also called display wait .
time.sleep(30) # Wait for a fixed length of time before continuing

But this approach is obviously unreasonable , Because the duration is fixed , The loading time is not always . Although I used this before , But this can highlight my progress , Hey ~~

2、 Browser level waiting is also called implicit waiting , This way is different from , Time is the greatest constant , But I don't think there's much difference between waiting and waiting , Of course, I didn't use this method .
self.chrome.implicitly_wait(30) # Wait for the specified maximum time before continuing
3、WebDriverWait Modular Wait function ,webdriver Module related built-in functions . Difference between implicit waiting , In this way, the conditions can be limited , Continue when a condition is met within a specified time .
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as Wait
# Wait for the browser for the longest 60 second , know id by idSIButton9 The element of is loaded 
Wait(self.chrome, 60).until(EC.presence_of_element_located((By.ID, "idSIButton9")))

stay get Before we get to this knowledge point , I always write my own circle to judge , You can live a few more years with this feeling . Hey ~~
There are other ways to choose according to your own needs , You can refer to this blog https://www.cnblogs.com/qingbaobei7370/p/11002840.html

3、 ... and 、 Browser startup parameter settings

There's nothing to say about this. Just post the code ~

# options = Options() # Creating configuration objects 
# options.add_argument('--window-size=1300,900') # Configure browser window size 
# options.add_argument('--incognito') # Configure the browser theme to black 
# options.add_argument('--ignore-certificate-errors') # Ignore connection warnings 
# options.add_experimental_option("detach", True) # Do not automatically close the browser 
# options.add_argument("--headless") # Set windowless mode 
# options.add_argument('--start-maximized') # Initial maximization 
# Disable the prompt that the browser is being controlled by an automated program 
# options.add_experimental_option('excludeSwitches', ['enable-automation'])
# options.add_experimental_option('useAutomationExtension', False) # Disable automatic extension 
# options.add_extension('vimm_chrome_proxyauth_plugin.zip') # Add proxy plug-ins 
# options.add_argument('--incognito') # Stealth mode ( Traceless mode )
# options.add_argument('--disable-javascript') # Ban javascript
# options.add_argument('--user-agent=Mozilla/5.0 HAHA') # Configuration object add substitution User-Agent The order of 
# options.add_argument('--disable-javascript') # Ban javascript
# options.add_argument("--disable-gpu") # Ban gpu
# Disable loading pictures 
# options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) 

Another point is about the problem of browser exit ,chrome.quit() Function to exit the browser , But I found that after the browser was closed , The background driver did not exit , As a result, a driver runs in the background every time . For this reason, I also record here , Exit the drive method .

There are two main types of exit driving me to Baidu , One is to close the driver through the process ; The other is to start the driver service first , Start the browser , Finally, exit the driver service . I use the second one here :

service = Service('chromedriver.exe') # Create a Google browser driven service 
service.command_line_args()
service.start() # Start the browser service 
webdriver.Chrome() # Launch the browser 

Four 、 Basic browser operation

Finally, I will review the basic operations related to the browser , Although this mission did not use .

# 1、 Pop ups 
webdriver.switch_to.alert.accept() # Accept pop ups 
webdriver.switch_to.alert.dismiss() # Cancel pop-up 
webdriver.switch_to.alert.text # Get pop-up information 
...
# 2、 Tab processing 
webdriver.window_handles # Get all tabs 
webdriver.switch_to.window(window_handles[index]) # Switch to a tab 
webdriver.close() # Close the current tab 
webdriver.execute_script(f"window.open()") # New tab 
...
# 3、 Browser cache processing 
# Clear cache 
webdriver.get('chrome://settings/clearBrowserData') # Jump to the browser to clear the cache settings 
webdriver.find_element_by_xpath('//settings-ui').send_keys(Keys.ENTER) # Analog key input Enter
...
# 4、 Screenshot of browser 
webdriver.get_screenshot_as_file(save_path) # A screenshot of the browser window and save it to the specified location 
# 5、 Close the browser 
webdriver.quit() # Exit the relevant driver , And close all windows 

Common operations are summarized here first , I will summarize some difficult pits encountered later . Welcome to discuss , If there are any problems, please correct them .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved