用 Python 捕获 Selenium 中的所有选项

原文:https://www . geesforgeks . org/capture-all-options-in-selenium-with-python/

先决条件: 使用Selenium的浏览器自动化

Selenium是通过程序控制互联网浏览器的有效设备。它对所有浏览器都是有目的的,适用于所有基本操作系统,其脚本是用许多语言编写的,即 Python、Java、C#等,我们将使用 Python。

要求:

需要从这里下载安装 chrome 驱动点击这里设置路径。

使用下拉列表:

首先你要导入选择类,然后你要做选择类的案例。完成选择类的案例后,您可以在该场合执行选择策略,从下拉列表中选择选项。

导入选择类:

from selenium.webdriver.support.ui import Select

查找选项长度:

drop=Select(driver.find_element_by_id(' ')

print(len(drop.options))

循序渐进法:

  • Selenium模块导入网络驱动程序

Python 3

# Import required module
from selenium import webdriver
  • 导入选择类模块。

Python 3

# Importing Select class
from selenium.webdriver.support.ui import Select
  • 使用网页进行下拉列表(例如:网址 )
  • 导航选项栏的 id。

  • 开始捕捉所有选项的循环。

以下是上述方法的完整程序:

Python 3

# Import required module
import time
from selenium import webdriver

# Import Select class
from selenium.webdriver.support.ui import Select

# Using chrome driver
driver = webdriver.Chrome()

# Web page url
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407")

# Find id of option
x = driver.find_element_by_id('RESULT_RadioButton-9')
drop = Select(x)

# Count number of options
print(len(drop.options))

# Capture all the options
for i in drop.options:
    print(i.text)

driver.close()

输出:

注意:一个空格是,所以只打印三个选项。