首页 > 解决方案 > Python selenium select option from dropdown

问题描述

I am trying to select an option from http://www.lacoteargus.ma/cote-maroc/recherche/
But I am getting this error:
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable: Element is not currently visible and may not be manipulated
in this line
Brands.select_by_visible_text('BMW')


Here is my script:

from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
import os
import time

Email = '***************'
Pass = '******'
LoginUrl = 'http://www.lacoteargus.ma/'

chrome_options = Options()
chromedriver_path = os.path.join(os.getcwd(), "chromedriver")
driver = webdriver.Chrome(executable_path=chromedriver_path, options=chrome_options)

driver.get(LoginUrl)
driver.find_element_by_name('strLogin').send_keys(Email)
driver.find_element_by_name('strPwd').send_keys(Pass)
driver.find_element_by_id('validation').click()
time.sleep(10)
driver.find_element_by_class_name('caret').click()
time.sleep(1)

Brands = Select(driver.find_element_by_id('marque'))
Brands.select_by_visible_text('BMW')

I have also tried:
Brands.select_by_value('1')
and
Brands.select_by_index('1')

None of them are working.

标签: python-3.xseleniumselectselenium-chromedriverhtml-select

解决方案


它不是选择元素,选择元素是隐藏的。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os

Email = '*****************'
Pass = '******'
LoginUrl = 'http://www.lacoteargus.ma/'

chrome_options = Options()
chromedriver_path = os.path.join(os.getcwd(), "chromedriver")
driver = webdriver.Chrome(executable_path=chromedriver_path, options=chrome_options)

driver.get(LoginUrl)
driver.find_element_by_name('strLogin').send_keys(Email)
el = driver.find_element_by_name('strPwd')
el.send_keys(Pass)
el.submit()

driver.find_element_by_class_name('caret').click()
driver.find_element_by_xpath(f"//span[text()='BMW']").click()

推荐阅读