日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

python+selenium七:下拉框、选项框、select用法

發(fā)布時間:2025/3/15 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python+selenium七:下拉框、选项框、select用法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

# from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Firefox()
url = "https://www.baidu.com"
driver.get(url)
time.sleep(3)


1、下拉框
mouse = driver.find_element("link text", "設(shè)置")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(0.5)
driver.find_element("link text", "搜索設(shè)置").click()
time.sleep(1)


方法一:直接定位
# 選擇下拉框選項的第三項
driver.find_element_by_xpath(".//*[@id='nr']/option[3]").click()
# 若此時點擊后,下拉選項未收回,可點擊整個下拉框,收回下拉選項
driver.find_element_by_xpath(".//*[@id='nr']").click()

?

方法二:二次定位
# 第一步:定位下拉框
parent = driver.find_element_by_id("nr")
# 第二步:在下拉框中,定位子元素,并操作
parent.find_element_by_xpath('.//option[@value="20"]').click()

?

?

select用法:


from selenium.webdriver.support.select import Select
# 先定位到下拉框
s = driver.find_element_by_id("nr")

?

第一種:根據(jù)索引定位(從0開始)
Select(s).select_by_index(0)
# 收回下拉選項
s.click()

?

第二種:根據(jù)value屬性定位
# 如:value = 50
Select(s).select_by_value("50")
# 收回下拉選項
s.click()

?

第三種:根據(jù)選項內(nèi)容定位
Select(s).select_by_visible_text("每頁顯示20條")
# 收回下拉選項
s.click()

?

?

選項框:
選項框的另外一種形式(這種不叫select,跟普通定位一樣)

?

轉(zhuǎn)載于:https://www.cnblogs.com/zhongyehai/p/9174639.html

總結(jié)

以上是生活随笔為你收集整理的python+selenium七:下拉框、选项框、select用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。