下拉菜单中的Python硒选择选项

时间:2019-04-13 10:35:54

标签: python-3.x selenium select selenium-chromedriver html-select

我正在尝试从 http://www.lacoteargus.ma/cote-maroc/recherche/ 中选择一个选项
但是我遇到了这个错误:
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable: Element is not currently visible and may not be manipulated
在此行
Brands.select_by_visible_text('BMW')


这是我的脚本:

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

我也尝试过:
Brands.select_by_value('1')

Brands.select_by_index('1')

它们都不起作用。

2 个答案:

答案 0 :(得分:1)

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

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()

答案 1 :(得分:1)

关于如何在引导日期选择器上选择日期:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "datemec")))
el = driver.find_element_by_xpath("//*[@id='datemec']/parent::div/span")
el.click()

n = 0
while True:
    driver.find_element_by_xpath("//div[contains(@class, 'datepicker-years')]//th[contains(@class,'prev')]").click()
    el = driver.find_element_by_xpath("//span[@class='input-group-addon']")
    try:
        el.find_element_by_xpath(f"//span[text()='2000']").click()
        break
    except Exception as e:
        if n > 3:
            raise
        n += 1