久久久精品2019免费观看_亚洲国产精品成人久久久_69国产成人综合久久精品91_国产精品久久精品视

python 用selenium選定下拉框?qū)嵗?/a>

用selenium選定下拉框適用html標(biāo)簽select

下面一個簡單的車型選擇框,我們需要利用selenium選定自己想要的類型

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
</head>
<body>
<select name="selecta" id="selectA">
    <option value="dz">大眾</option>
    <option value="bm">寶馬</option>
    <option value="ad">奧迪</option>
    <option value="lh">路虎</option>
</select>
</body>
</html>

下面是具體的python代碼:

# coding:utf-8
# 1.導(dǎo)入selenium
from selenium import webdriver
from time import sleep
import os
from selenium.webdriver.support.select import Select
# 2.打開瀏覽器
driver = webdriver.Chrome()
# 3.打開頁面
url = 'file:///C:/phpstudy_pro/WWW/readme.html'
driver.get(url)
sleep(2)
# 4.使用Select類操作下拉框
# 4.1 定位下拉框標(biāo)簽
selectA = driver.find_element(by='id', value="selectA")
# 4.2 通過Select類選擇選項
# 創(chuàng)建下拉框?qū)ο?# Select(定位下拉框的元素--select標(biāo)簽元素)
select = Select(selectA)
# 通過value值選擇選項(選擇路虎)
select.select_by_value("lh")
sleep(2)

注:
本selenium選定下拉框?qū)嵗玫膒ython版本是3.10,如果你的版本比較低,那么需要把
selectA = driver.find_element(by='id', value="selectA")
換成
selectA = driver.find_element_by_id("selectA")







未經(jīng)允許不得轉(zhuǎn)載:445IT之家 » python 用selenium選定下拉框?qū)嵗?/a>

贊 (0) 打賞

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

微信掃一掃打賞