英文:
How do I obtain a list of values from a website dropdown using selenium?
问题
我正在尝试获取名为“State”的下拉菜单中的所有州的名称。
然而,问题是,我试图生成的列表并没有返回下拉菜单中的名称,而是返回了一列空值。它确实正确获取了下拉菜单中的元素数量,但元素的值是空白的。
我做错了什么?我应该如何修改我的代码以获得期望的结果?
我目前正在使用selenium,以下是我的代码:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
url = "https://vahan.parivahan.gov.in/vahan4dashboard/vahan/view/reportview.xhtml"
driver.get(url)
dropdown_element = driver.find_element(By.ID,"j_idt31_input")
dropdown = Select(dropdown_element)
option_names = [option.text for option in dropdown.options]
print(len(option_names))
print(option_names)
driver.quit()
英文:
I am trying to get the names of all State from a respective dropdown named State
.
However, the problem is, the list that I'm trying to generate doesn't return the names in the dropdown instead I'm getting a list of blank values. It does get the number of elements in the dropdown right, but the elements' values are blank.
What am I doing wrong? and how can I modify my code to get the desired result?
I am currently using selenium and the following is my code:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
url = "https://vahan.parivahan.gov.in/vahan4dashboard/vahan/view/reportview.xhtml"
driver.get(url)
dropdown_element = driver.find_element(By.ID,"j_idt31_input")
dropdown = Select(dropdown_element)
option_names = [option.text for option in dropdown.options]
print(len(option_names))
print(option_names)
driver.quit()
答案1
得分: 1
你唯一需要更改的是,你应该使用方法.get_attribute('innerHTML')
而不是.text
来获取Select
下的option
标签中的文本。
以下是完整的解决方案:
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
driver = Chrome()
url = "https://vahan.parivahan.gov.in/vahan4dashboard/vahan/view/reportview.xhtml"
driver.get(url)
dropdown_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "j_idt31_input")))
dropdown = Select(dropdown_element)
option_names = [option.get_attribute('innerHTML') for option in dropdown.options]
print(len(option_names))
print(option_names)
输出:
35
['All Vahan4 Running States (34/36)', 'Andaman & Nicobar Island(8)', 'Andhra Pradesh(80)', 'Arunachal Pradesh(26)', 'Assam(36)', 'Bihar(49)', 'Chhattisgarh(30)', 'Chandigarh(1)', 'UT of DNH and DD(3)', 'Delhi(23)', 'Goa(13)', 'Gujarat(37)', 'Himachal ...
英文:
The only thing you need to change is that you should use the method .get_attribute('innerHTML')
instead of .text
in order to get the text from the option
tags under the Select
.
Here's the complete solution:
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
driver = Chrome()
url = "https://vahan.parivahan.gov.in/vahan4dashboard/vahan/view/reportview.xhtml"
driver.get(url)
dropdown_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "j_idt31_input")))
dropdown = Select(dropdown_element)
option_names = [option.get_attribute('innerHTML') for option in dropdown.options]
print(len(option_names))
print(option_names)
output:
35
['All Vahan4 Running States (34/36)', 'Andaman & Nicobar Island(8)', 'Andhra Pradesh(80)', 'Arunachal Pradesh(26)', 'Assam(36)', 'Bihar(49)', 'Chhattisgarh(30)', 'Chandigarh(1)', 'UT of DNH and DD(3)', 'Delhi(23)', 'Goa(13)', 'Gujarat(37)', 'Himachal Pradesh(113)', 'Haryana(179)', 'Jharkhand(30)', 'Jammu and Kashmir(21)', 'Karnataka(68)', 'Kerala(87)', 'Ladakh(3)', 'Maharashtra(53)', 'Meghalaya(13)', 'Manipur(12)', 'Madhya Pradesh(52)', 'Mizoram(10)', 'Nagaland(9)', 'Odisha(39)', 'Punjab(93)', 'Puducherry(8)', 'Rajasthan(142)', 'Sikkim(8)', 'Tamil Nadu(146)', 'Tripura(9)', 'Uttarakhand(21)', 'Uttar Pradesh(78)', 'West Bengal(56)']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论