SoFunction
Updated on 2024-10-26

Python crawler implementation of Selenium drop-down box processing

When we browse the web page will often encounter drop-down boxes, WebDriver provides a Select class to deal with drop-down boxes, please see below for details:

The key methods used in this chapter are as follows:

  • select_by_value(): set the value of the dropdown box
  • switch_to.(): locates and accepts an existing warning box (for more details seePython Crawler - Selenium (9) Warning Box (Popup) Handling)
  • click(): mouse click event (for other mouse events, please refer toPython Crawler - Selenium (5) Mouse Events)
  • move_to_element(): mouse hover (for more details, please seePython Crawler - Selenium (5) Mouse Events)
from selenium import webdriver
from  import ActionChains
from  import Select
import time
driver = ()
('')

# Hover over the "Settings" link.
link = driver.find_element_by_link_text('Settings')
ActionChains(driver).move_to_element(link).perform()
(2) #Sleep for two seconds and see the effect

# Open search settings
driver.find_element_by_link_text("Search Settings").click()
(2) #Sleep for two seconds and see the effect

# of search results displayed
sel = driver.find_element_by_xpath("//select[@id='nr']")
Select(sel).select_by_value('50') # 50 displayed
(2) #Sleep for two seconds and see the effect

# Save settings
driver.find_element_by_class_name("prefpanelgo").click()
(2) #Sleep for two seconds and see the effect

# Locate and accept existing warning boxes
alert = driver.switch_to.()
(2) #Sleep for two seconds and see the effect

()

List of functions in the select class

function (math.) analyze
options Returns all the options of the select element.
all_selected_options Returns all selected options in the select element.
first_selected_option Returns the first option selected in the select element.
select_by_index(index) Positioning by index, index starts at "0".
select_by_value(value) Positioning by value attribute value
select_by_visible_text(text)t Positioned by text value, visible_text is the value in the middle of the option tag, i.e. the value displayed in the dropdown box;
deselect_all() Cancel all selected items
deselect_by_index(index) Unselected index entries
deselect_by_value(value) Unchecks the selected value.
deselect_by_visible_text(text) Cancel selected text values

give an example

html below:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>I'm the title.</title>
</head>
<body>
<!--selecttab (of a window) (computing)-->
<select name="city" size="5" multiple="multiple">
 <option value="1" tabindex="1">Beijing, capital of People's *</option>
 <option value="2" tabindex="2" selected="selected">He'nan Mengguzu autonomous county in Qinghai</option>
 <option value="3" tabindex="3">anhui</option>
 <option value="4" tabindex="4">Shandong</option>
 <option value="5" tabindex="5">Shanghai</option>
</select>

</body>
</html>

from selenium import webdriver
from  import Select
import time

driver = (r"D:\browser\chromedriver\")
("http://localhost:63342/ui_test/select%E6%A0%87%E7%AD%")

driver.maximize_window()

ele = driver.find_element_by_name("city")
select = Select(ele)
select.select_by_value("3") # Select "Hebei"
(3)
select.select_by_index(0) # Select "Beijing"
(3)
select.deselect_by_value("3") # Uncheck "Hebei"
(3)
select.deselect_by_index(0) # Uncheck "Beijing"
(3)
()

Selenium Anthology Portal:

caption synopsis
Python Crawler - Selenium (1) Installation and Simple Use Detailed description of Selenium's dependencies on Windows and Centos7 installation and simple use of the environment
Python Crawler - Selenium (2) element positioning and WebDriver common methods Detailed introduction to the 8 ways to locate an element and use it in conjunction with methods such as click and type, submit, get assertion information, etc.
Python Crawler - Selenium (3) Common Ways to Control Your Browser Detailed introduction to the use of customizing the browser window size or full screen, controlling the browser backward, forward, refresh the browser and other methods
Python Crawler - Selenium (4) Configuring Startup Item Parameters Detailed description of Selenium startup parameters configuration, including no interface mode, browser window size settings, browser User-Agent (request header) and so on.
Python Crawler - Selenium (5) Mouse Events Detailed introduction to the use of mouse right-click, double-click, drag, mouse hover and other methods
Python Crawler - Selenium (6) Keyboard Events Detailed introduction to keyboard operation, including almost all commonly used keys and key combinations
Python Crawler - Selenium (7) Multi-Window Switching Detailed description of how Selenium realizes the freedom to switch between different windows
Python Crawler - Selenium (8) frame/iframe form nested pages Details on how to switch from the currently positioned body to a frame/iframe form in an inline page
Python Crawler - Selenium (9) Warning Box (Popup) Handling Details on how to locate and handle multiple types of warning pop-ups
Python Crawler - Selenium (10) Dropdown Box Processing Details on how to flexibly position and handle drop-down boxes
Python Crawler - Selenium (11) File Uploads Details how to elegantly specify files to upload via send_keys()
Python Crawler - Selenium (12) Getting Login Cookies and Adding Cookies to Automate Login Details on how to get cookies and use them for automatic login
Python Crawler - Selenium (13) Setting Up Element Waiting Details on how to elegantly set the element wait time to prevent the program from running too fast and causing the element positioning failure
Python Crawler - Selenium (14) Window Screenshots Detailed instructions on how to use window screenshots
Python Crawler - Selenium (15) Close Browser Detailed description of the difference between the two types of closing windows

to this article on the Python crawler Selenium drop-down box processing implementation of the article is introduced to this, more related Selenium drop-down box content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!