如何使用Selenium从网站下拉菜单中获取值列表?

huangapple go评论47阅读模式
英文:

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)']

huangapple
  • 本文由 发表于 2023年6月22日 09:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定