Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.


Popup window handle

JavaScript has three types of popovers: Alert, Confirm, and prompt.

Solution: Locate (switch_to.alert automatically obtains the current popup window), and then use text, ACCEPT, dismiss, send_keys and other methods to operate

methods describe
text Gets the text in the popover
accept Accept (confirm) popover content
dismiss Remove (cancel) popover
send_keys Sends text to the warning box

Here’s a simple test page with three buttons for three pop-ups.

<! DOCTYPE html> <html lang="en"> <head> </head> <body> <button id="alert">alert</button> <button id="confirm">confirm</button> <button id="prompt">prompt</button> <script type="text/javascript"> const dom1 = document.getElementById("alert") dom1.addEventListener('click', function(){ alert("alert hello") }) const dom2 = document.getElementById("confirm") dom2.addEventListener('click', function(){ confirm("confirm hello") }) const dom3 = document.getElementById("prompt") dom3.addEventListener('click', function(){ prompt("prompt hello") }) </script> </body> </html>Copy the code

Now use the above method to test. In order to prevent the popover operation too fast, every time the popover operation, usesleepForce a wait period.

from selenium import webdriver from pathlib import Path from time import sleep driver = webdriver.Firefox() driver.get('file:///' + str(Path(Path.cwd(), Find_element_by_xpath ('//*[@id="alert"]').click() sleep(1) alert = Print (alert.text) # confirm alert.accept() sleep(2) # Click Confirm driver.find_element_by_xpath('//*[@id="confirm"]').click() sleep(1) confirm = driver.switch_to.alert print(confirm.text) Find_element_by_xpath ('//*[@id="prompt"]').click() sleep(1) prompt Send prompt. Send_keys ("Dream, Killer") sleep(2) = driver.switch_to.alert print(prompt. Text) Prompt. Accept () "" output alert hello confirm hello prompt hello" "Copy the code

Note: Careful readers should notice that the browser for this operation isFirefoxWhy notChrome? The reason is that the test finds executionpromptsend_keysCannot fill the input box with text. After trying various methods and looking at the source code, it was confirmed that the problem was not the code, and later learned from other sources that the cause might beChromeThe version andseleniumVersion of the problem, but there is no very convenient solution, so do not continue to delve into, insteadFirefoxIt can run successfully. Let me record mine hereChromeVersion, if there is a big man who knows how to inChromeTo solve this problem, please guide in the comments section, thanks in advance! Selenium: 3.141.0 Chrome: 94.0.4606.71

Upload & download files

Upload a file

Common web page uploads usually use input tags or plug-ins (JavaScript, Ajax). For input tag uploads, you can directly use send_keys(path) to upload. Start by writing a test page.

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta  name="viewport" content="width=device-width, "> <title>Document</title> </head> <body> <input type="file" name=""> </body> </ HTML >Copy the code

The following throughxpathpositioninginputTag, and then usesend_keys(str(file_path)Upload files.

from selenium import webdriver from pathlib import Path from time import sleep driver = webdriver.Chrome() file_path = Path(Path.cwd(), 'upload download. HTML) driver. The get (' file:/// + STR (file_path)) driver.find_element_by_xpath('//*[@name="upload"]').send_keys(str(file_path))Copy the code

The download file

Chrome

To enable the file download in Firefox, add the prefs parameter through add_experimental_option.

  • download.default_directory: Sets the download path.
  • profile.default_content_settings.popups: 0 Disables the pop-up window.

The following test download sogou pictures. Specify the save path as the code path.

from selenium import webdriver 
​
prefs = {'profile.default_content_settings.popups': 0,
         'download.default_directory': str(Path.cwd())}
option = webdriver.ChromeOptions() 
option.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options=option)
driver.get("https://pic.sogou.com/d?query=%E7%83%9F%E8%8A%B1&did=4&category_from=copyright")
driver.find_element_by_xpath('/html/body/div/div/div/div[2]/div[1]/div[2]/div[1]/div[2]/a').click()
​
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_xpath('./html').send_keys('thisisunsafe')
Copy the code

“Thisisunsafe” is what you’ll need to type when you pop up a link like the following, “Your connection is not a private connection.” The keyboard input string operation is called send_keys, but since the TAB is newly opened, switch the window to the latest TAB with switch_to.window().

The Firefox browser

For Firefox to download files, you need to set some properties of FirefoxProfile () through set_preference.

  • browser.download.foladerList0 indicates the default download path. 2 Save the configuration file to the specified directory.
  • browser.download.dir: Specifies the download directory.
  • browser.download.manager.showWhenStarting: Whether to display start,booleanType.
  • browser.helperApps.neverAsk.saveToDisk: No dialog box is displayed for the specified file type.
  • HTTP Content-Type mapping table: www.runoob.com/http/http-c…
from selenium import webdriver
import os
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.dir",os.getcwd())
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showhenStarting",True)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream")
​
driver = webdriver.Firefox(firefox_profile = fp)
driver.get("https://pic.sogou.com/d?query=%E7%83%9F%E8%8A%B1&did=4&category_from=copyright")
driver.find_element_by_xpath('/html/body/div/div/div/div[2]/div[1]/div[2]/div[1]/div[2]/a').click()
Copy the code

It works basically the same as Chrome, which I won’t show you here.

Cookies operation

Cookies are the key to identify whether a user is logged in or not. Selenium + Requests is often used in crawlers to implement cookie persistence, that is, Selenium simulated login is first used to obtain cookies, and then requests are carried out through Requests.

Webdriver provides several operations for cookies: read, add and delete.

  • get_cookies: returns what is visible in the current session as a dictionarycookieInformation.
  • get_cookie(name)Returns thecookieIn the dictionarykey == namecookieInformation.
  • add_cookie(cookie_dict)Will:cookieAdd to the current session
  • delete_cookie(name): Deletes a single namecookie.
  • delete_all_cookies(): Deletes all items in the range of the sessioncookie.

Let’s look at a simple example that demonstrates their use.

From Selenium import webDriver driver = webdriver.chrome () driver.get("https://blog.csdn.net/") # print(driver.get_cookies()) cookie_dict = { 'domain': '.csdn.net', 'expiry': 1664765502, 'httpOnly': False, 'name': 'test', 'path': '/', 'secure': True, 'value': Add_cookie (cookie_dict) # print(driver.get_cookie('test')) # delete Driver.delete_cookie ('test') # delete all cookies in the current session driver.delete_all_cookies()Copy the code

Invoke JavaScript

Webdriver uses JavaScript to handle the scroll bar, and can also enter text into a Textarea text field (WebDriver can only locate, not enter text). JavaScript execution is implemented in WebDriver using the execute_script method.

Sliding scroll bar

I’m sliding through x and y

For this method of sliding by coordinate, we need to know that the starting position of the table is in the top left corner of the page (0,0). Let’s take a look at an example, sliding CSDN home page.

from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.get("https://blog.csdn.net/") Sleep (1) the js = "window. The scrollTo (0500);" driver.execute_script(js)Copy the code

Slide by reference label

In this way, you need to find a reference label and slide the scroll bar to the position of the label. The following is the CSDN home page as an example, we use the cycle to achieve repeated sliding. The li tag is actually lazy loading, as the user slides to the last tag before loading the following data.

from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.get("https://blog.csdn.net/") sleep(1) driver.implicitly_wait(3) for i in range(31, 102, 10): sleep(1) target = driver.find_element_by_xpath(f'//*[@id="feedlist_id"]/li[{i}]') driver.execute_script("arguments[0].scrollIntoView();" , target)Copy the code

Other operating

Close all pages

Use the quit() method to close all Windows and exit the driver.

driver.quit()
Copy the code

Close the current page

Use the close() method to close the current page. Pay attention to the word “current page” when using it. When you close a new page, you need to switch Windows to operate on the new window and close it. Let’s see if we can close a newly opened page without switching Windows.

from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.get('https://blog.csdn.net/') Driver.find_element_by_xpath ('//*[@id="mainContent"]/aside/div[1]/div').click() # Driver.switch_to.window (driver.window_handles[-1]) sleep(3) driver.close()Copy the code

As you can see, without switching Windows,driverThe object is still the page at the beginning of the operation.

Take a screenshot of the current page

In wendriver, get_screenshot_as_file() is used to take a screenshot of the “current page”. Here, as in the close() method above, it is necessary to switch Windows for new operations, otherwise the screenshot will be the same as the original page. Try except with get_screenshot_as_file() is used to capture screenshots of the page, which are mainly used to record error pages when we test.

try:
    driver.find_element_by_xpath('//*[@id="mainContent"]/aside/div[1]/div').click()
except:
    driver.get_screenshot_as_file(r'C:\Users\pc\Desktop\screenshot.png')
Copy the code

For those who are new to Python or want to learn Python, you can search “Python New Horizons” on wechat to communicate and learn with others. They are all beginners. Sometimes a simple question is stuck for a long time, but others may suddenly realize it with a little help.