python-3.x - 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.
解决方案
它不是选择元素,选择元素是隐藏的。
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()
推荐阅读
- python - Flask Python 中的动态绘图可视化(或图像)
- c# - 在c#中添加winform用户控件事件
- javascript - 使用 HTML 中的 JS 自动调整文本大小以适应图像容器
- django - 如何将我的 Web 应用程序的本地数据库同步到我部署的 Web 应用程序的数据库?
- firewall - 如何创建允许我制定基于应用程序的规则的防火墙?
- python - 使用Python密码保护xlsx Excel文件
- java - 为什么从 ProductDetailsActivity 切换到 HomeActivity 时,LoginActivity 中会出现 toast?
- google-sheets - 数据工作室过滤器 - 一些字符串数据自动转换为日期范围
- debugging - 使用单 GPU 和 Visual Studio 进行 Cuda 调试
- javascript - 如何在打开随机播放的情况下跳到下一首歌曲?反应原生