尝试使用 python 和 selenium select 下拉菜单时出现问题
Issues while trying to select a dropdown with python and selenium
大家好(这里是python新手),
我正在尝试 select 来自以下站点的下拉菜单 [(此处为图片)][1] 的值:
https://www.amfiindia.com/net-asset-value/nav-history
但我收到错误
ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated (Session info: chrome=92.0.4515.159)
这是我目前的代码:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
url = "https://www.amfiindia.com/net-asset-value/nav-history"
driver = webdriver.Chrome("chromedriver.exe")
driver.get(url)
dropdown_select = Select(driver.find_element_by_id("NavHisMFName"))
我尝试使用以下方法:select_by_value、select_by_visible_text、select_by_index,但其中 none 有效。
dropdown_select.select_by_value("6")
dropdown_select.select_by_visible_text("DSP Mutual Fund")
dropdown_select.select_by_index(14)
此外,我尝试单击下拉按钮,然后尝试 select 一个选项,但仍然没有结果。最后,我尝试点击下拉按钮设置焦点并输入但仍然没有成功。
我所做的是使用以下代码获取下拉选项的所有值、文本和索引:
options = dropdown_select.options
for index in range(0, len(options)-1):
print("Value:", options[index].get_attribute("value"), "Text:",
options[index].get_attribute("text"), "Index:", options[index].get_attribute("index"))
我得到了以下结果:
Value: Text: --Select Mutual Fund House-- Index: 0
Value: 39 Text: ABN AMRO Mutual Fund Index: 1
Value: 3 Text: Aditya Birla Sun Life Mutual Fund Index: 2
Value: 50 Text: AEGON Mutual Fund Index: 3
Value: 1 Text: Alliance Capital Mutual Fund Index: 4
Value: 53 Text: Axis Mutual Fund Index: 5
Value: 4 Text: Baroda Mutual Fund Index: 6
Value: 36 Text: Benchmark Mutual Fund Index: 7
Value: 59 Text: BNP Paribas Mutual Fund Index: 8
Value: 46 Text: BOI AXA Mutual Fund Index: 9
Value: 32 Text: Canara Robeco Mutual Fund Index: 10
Value: 60 Text: Daiwa Mutual Fund Index: 11
Value: 31 Text: DBS Chola Mutual Fund Index: 12
Value: 38 Text: Deutsche Mutual Fund Index: 13
Value: 6 Text: DSP Mutual Fund Index: 14
Value: 47 Text: Edelweiss Mutual Fund Index: 15
Value: 40 Text: Fidelity Mutual Fund Index: 16
Value: 51 Text: Fortis Mutual Fund Index: 17
Value: 27 Text: Franklin Templeton Mutual Fund Index: 18
Value: 8 Text: GIC Mutual Fund Index: 19
Value: 49 Text: Goldman Sachs Mutual Fund Index: 20
Value: 9 Text: HDFC Mutual Fund Index: 21
Value: 37 Text: HSBC Mutual Fund Index: 22
Value: 20 Text: ICICI Prudential Mutual Fund Index: 23
Value: 57 Text: IDBI Mutual Fund Index: 24
Value: 48 Text: IDFC Mutual Fund Index: 25
Value: 68 Text: IIFCL Mutual Fund (IDF) Index: 26
Value: 62 Text: IIFL Mutual Fund Index: 27
Value: 11 Text: IL&F S Mutual Fund Index: 28
Value: 65 Text: IL&FS Mutual Fund (IDF) Index: 29
Value: 63 Text: Indiabulls Mutual Fund Index: 30
Value: 14 Text: ING Mutual Fund Index: 31
Value: 42 Text: Invesco Mutual Fund Index: 32
Value: 70 Text: ITI Mutual Fund Index: 33
Value: 16 Text: JM Financial Mutual Fund Index: 34
Value: 43 Text: JPMorgan Mutual Fund Index: 35
Value: 17 Text: Kotak Mahindra Mutual Fund Index: 36
Value: 56 Text: L&T Mutual Fund Index: 37
Value: 18 Text: LIC Mutual Fund Index: 38
Value: 69 Text: Mahindra Manulife Mutual Fund Index: 39
Value: 45 Text: Mirae Asset Mutual Fund Index: 40
Value: 19 Text: Morgan Stanley Mutual Fund Index: 41
Value: 55 Text: Motilal Oswal Mutual Fund Index: 42
Value: 54 Text: Navi Mutual Fund Index: 43
Value: 21 Text: Nippon India Mutual Fund Index: 44
Value: 73 Text: NJ Mutual Fund Index: 45
Value: 58 Text: PGIM India Mutual Fund Index: 46
Value: 44 Text: PineBridge Mutual Fund Index: 47
Value: 34 Text: PNB Mutual Fund Index: 48
Value: 64 Text: PPFAS Mutual Fund Index: 49
Value: 10 Text: Principal Mutual Fund Index: 50
Value: 13 Text: quant Mutual Fund Index: 51
Value: 41 Text: Quantum Mutual Fund Index: 52
Value: 74 Text: Samco Mutual Fund Index: 53
Value: 22 Text: SBI Mutual Fund Index: 54
Value: 52 Text: Shinsei Mutual Fund Index: 55
Value: 67 Text: Shriram Mutual Fund Index: 56
Value: 2 Text: Standard Chartered Mutual Fund Index: 57
Value: 24 Text: SUN F&C Mutual Fund Index: 58
Value: 33 Text: Sundaram Mutual Fund Index: 59
Value: 25 Text: Tata Mutual Fund Index: 60
Value: 26 Text: Taurus Mutual Fund Index: 61
Value: 72 Text: Trust Mutual Fund Index: 62
Value: 61 Text: Union Mutual Fund Index: 63
Value: 28 Text: UTI Mutual Fund Index: 64
Value: 71 Text: YES Mutual Fund Index: 65
有人可以帮我解决这个问题吗?先感谢您!祝你度过愉快的一周。
[1]: https://i.stack.imgur.com/2uT7v.png
您可以使用 execute_script
更改 select
元素中的 display: none
driver.execute_script("document.getElementById('NavHisMFName').style = '';")
而且 select 因为它是可见的,但它看起来可能很奇怪
一种方法是输入文本而不是从下拉列表中选择。
d.get("https://www.amfiindia.com/net-asset-value/nav-history")
d.find_element_by_id("divNavHisMFName").find_element_by_tag_name("input").send_keys("DSP Mutual Fund")
ActionChains(d).send_keys(Keys.END).perform()
进口:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
大家好(这里是python新手), 我正在尝试 select 来自以下站点的下拉菜单 [(此处为图片)][1] 的值: https://www.amfiindia.com/net-asset-value/nav-history 但我收到错误
ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated (Session info: chrome=92.0.4515.159)
这是我目前的代码:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
url = "https://www.amfiindia.com/net-asset-value/nav-history"
driver = webdriver.Chrome("chromedriver.exe")
driver.get(url)
dropdown_select = Select(driver.find_element_by_id("NavHisMFName"))
我尝试使用以下方法:select_by_value、select_by_visible_text、select_by_index,但其中 none 有效。
dropdown_select.select_by_value("6")
dropdown_select.select_by_visible_text("DSP Mutual Fund")
dropdown_select.select_by_index(14)
此外,我尝试单击下拉按钮,然后尝试 select 一个选项,但仍然没有结果。最后,我尝试点击下拉按钮设置焦点并输入但仍然没有成功。
我所做的是使用以下代码获取下拉选项的所有值、文本和索引:
options = dropdown_select.options
for index in range(0, len(options)-1):
print("Value:", options[index].get_attribute("value"), "Text:",
options[index].get_attribute("text"), "Index:", options[index].get_attribute("index"))
我得到了以下结果:
Value: Text: --Select Mutual Fund House-- Index: 0
Value: 39 Text: ABN AMRO Mutual Fund Index: 1
Value: 3 Text: Aditya Birla Sun Life Mutual Fund Index: 2
Value: 50 Text: AEGON Mutual Fund Index: 3
Value: 1 Text: Alliance Capital Mutual Fund Index: 4
Value: 53 Text: Axis Mutual Fund Index: 5
Value: 4 Text: Baroda Mutual Fund Index: 6
Value: 36 Text: Benchmark Mutual Fund Index: 7
Value: 59 Text: BNP Paribas Mutual Fund Index: 8
Value: 46 Text: BOI AXA Mutual Fund Index: 9
Value: 32 Text: Canara Robeco Mutual Fund Index: 10
Value: 60 Text: Daiwa Mutual Fund Index: 11
Value: 31 Text: DBS Chola Mutual Fund Index: 12
Value: 38 Text: Deutsche Mutual Fund Index: 13
Value: 6 Text: DSP Mutual Fund Index: 14
Value: 47 Text: Edelweiss Mutual Fund Index: 15
Value: 40 Text: Fidelity Mutual Fund Index: 16
Value: 51 Text: Fortis Mutual Fund Index: 17
Value: 27 Text: Franklin Templeton Mutual Fund Index: 18
Value: 8 Text: GIC Mutual Fund Index: 19
Value: 49 Text: Goldman Sachs Mutual Fund Index: 20
Value: 9 Text: HDFC Mutual Fund Index: 21
Value: 37 Text: HSBC Mutual Fund Index: 22
Value: 20 Text: ICICI Prudential Mutual Fund Index: 23
Value: 57 Text: IDBI Mutual Fund Index: 24
Value: 48 Text: IDFC Mutual Fund Index: 25
Value: 68 Text: IIFCL Mutual Fund (IDF) Index: 26
Value: 62 Text: IIFL Mutual Fund Index: 27
Value: 11 Text: IL&F S Mutual Fund Index: 28
Value: 65 Text: IL&FS Mutual Fund (IDF) Index: 29
Value: 63 Text: Indiabulls Mutual Fund Index: 30
Value: 14 Text: ING Mutual Fund Index: 31
Value: 42 Text: Invesco Mutual Fund Index: 32
Value: 70 Text: ITI Mutual Fund Index: 33
Value: 16 Text: JM Financial Mutual Fund Index: 34
Value: 43 Text: JPMorgan Mutual Fund Index: 35
Value: 17 Text: Kotak Mahindra Mutual Fund Index: 36
Value: 56 Text: L&T Mutual Fund Index: 37
Value: 18 Text: LIC Mutual Fund Index: 38
Value: 69 Text: Mahindra Manulife Mutual Fund Index: 39
Value: 45 Text: Mirae Asset Mutual Fund Index: 40
Value: 19 Text: Morgan Stanley Mutual Fund Index: 41
Value: 55 Text: Motilal Oswal Mutual Fund Index: 42
Value: 54 Text: Navi Mutual Fund Index: 43
Value: 21 Text: Nippon India Mutual Fund Index: 44
Value: 73 Text: NJ Mutual Fund Index: 45
Value: 58 Text: PGIM India Mutual Fund Index: 46
Value: 44 Text: PineBridge Mutual Fund Index: 47
Value: 34 Text: PNB Mutual Fund Index: 48
Value: 64 Text: PPFAS Mutual Fund Index: 49
Value: 10 Text: Principal Mutual Fund Index: 50
Value: 13 Text: quant Mutual Fund Index: 51
Value: 41 Text: Quantum Mutual Fund Index: 52
Value: 74 Text: Samco Mutual Fund Index: 53
Value: 22 Text: SBI Mutual Fund Index: 54
Value: 52 Text: Shinsei Mutual Fund Index: 55
Value: 67 Text: Shriram Mutual Fund Index: 56
Value: 2 Text: Standard Chartered Mutual Fund Index: 57
Value: 24 Text: SUN F&C Mutual Fund Index: 58
Value: 33 Text: Sundaram Mutual Fund Index: 59
Value: 25 Text: Tata Mutual Fund Index: 60
Value: 26 Text: Taurus Mutual Fund Index: 61
Value: 72 Text: Trust Mutual Fund Index: 62
Value: 61 Text: Union Mutual Fund Index: 63
Value: 28 Text: UTI Mutual Fund Index: 64
Value: 71 Text: YES Mutual Fund Index: 65
有人可以帮我解决这个问题吗?先感谢您!祝你度过愉快的一周。 [1]: https://i.stack.imgur.com/2uT7v.png
您可以使用 execute_script
更改 select
元素中的 display: none
driver.execute_script("document.getElementById('NavHisMFName').style = '';")
而且 select 因为它是可见的,但它看起来可能很奇怪
一种方法是输入文本而不是从下拉列表中选择。
d.get("https://www.amfiindia.com/net-asset-value/nav-history")
d.find_element_by_id("divNavHisMFName").find_element_by_tag_name("input").send_keys("DSP Mutual Fund")
ActionChains(d).send_keys(Keys.END).perform()
进口:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains