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 .
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 .
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 :
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 ~~
self.chrome.implicitly_wait(30) # Wait for the specified maximum time before continuing
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
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
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 .