Selenium 시리즈(10) - Select 드롭다운 상자에 대한 조작 및 원본 해독
https://www.cnblogs.com/poloyy/category/1680176.html
그 다음에 만약에 네가 앞의 기초 지식을 모르면 스스로 보충해야 한다. 블로거는 당분간 총결산을 하지 않았다(나도 할 수 있지만 나는selenium을 배우면 앞부분을 복습하지 않아도 된다.ㅋㅋ...)
우선, 아래의 html 코드를 파일에 저장합니다
후속 코드 작은 사례는 모두 이 html에 방문한 DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>   title>
head>
<body>
<select id="pro">
    <option value="gd">  option>
    <option value="hb">  option>
    <option value="gj">  option>
select>
<select id="city" multiple>
    <option value="gz">  option>
    <option value="wh">  option>
    <option value="gj">  option>
select>
body>
html>  주의
select 드롭다운 상자에 멀티플 속성이 있으면 옵션을 많이 선택할 수 있지만, 이런 경우는 흔치 않습니다
드롭다운 상자 작업 정보
옵션 되돌리기 & 선택 조작
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__  =
__Time__   = 2020/3/25 17:52
__Author__ =        
__Blog__   = https://www.cnblogs.com/poloyy/
"""
from time import sleep
from selenium.webdriver.support.select import Select
from selenium import webdriver
driver = webdriver.Chrome("../resources/chromedriver.exe")
#  html          
driver.get("file:///C:/   .html")
driver.maximize_window()
#   select    
pro = Select(driver.find_element_by_id("pro"))
#       
for option in pro.options:
    print(option.text)
#           
for option in pro.all_selected_options:
    print(option.text)
#   value  
pro.select_by_value("bj")
sleep(1)
#   index  
pro.select_by_index(1)
sleep(1)
#         
pro.select_by_visible_text("  ")  작업 선택 취소
#   id=city    
city = Select(driver.find_element_by_id("city"))
#   
for option in city.options:
    if not option.is_selected():
        city.select_by_visible_text(option.text)
sleep(1)
#   value    
city.deselect_by_value("bj")
sleep(1)
#   index    
city.deselect_by_index(0)
sleep(1)
#         
city.deselect_by_visible_text("  ")
sleep(1)
#   
for option in city.options:
    if not option.is_selected():
        city.select_by_visible_text(option.text)
sleep(1)
#         
city.deselect_all()  지식점
취소 작업은 멀티플렉스가 추가된 드롭다운 상자에만 적용됩니다. 그렇지 않으면 오류가 발생합니다.
    raise NotImplementedError("You may only deselect options of a multi-select")
NotImplementedError: You may only deselect options of a multi-select  Select 소스 판독
class Select(object):
    def __init__(self, webelement):
        """
        Constructor. A check is made that the given element is, indeed, a SELECT tag. If it is not,
        then an UnexpectedTagNameException is thrown.
        :Args:
         - webelement - element SELECT element to wrap
        Example:
            from selenium.webdriver.support.ui import Select 
            Select(driver.find_element_by_tag_name("select")).select_by_index(2)
        """  지식점
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.