本文介绍了使用Selenium和python选择下拉菜单时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问一个页面,该页面要求我从下拉菜单中选择一个选项.

I am trying access a page which requires me to select an option from a drop down menu.

当我运行代码atm时,出现错误,提示它无法通过id定位下拉元素.我不知道如何解决这种情况,因为我正在复制并粘贴元素id.

When i run my code atm, I get an error where it says it was unable to locate the drop down element by id. I do not know how to remedy this situation, as I am copying and pasting the elements id.

from selenium import webdriver
from selenium.webdriver.support.select import Select
import time

driver = webdriver.Firefox()
driver.get('http://webapp.northampton.edu/coursesearch/default.aspx')
time.sleep(1)
dropdown = driver.find_element_by_id('pg0_V_ddlTerm')
select_box = Select(dropdown)
time.sleep(1)
select_box.select_by_value('2015;S2')

我也尝试过按名称选择,但这也没有结果.选择下拉菜单后,我尝试选择选项S22015.

I also tried selecting by name, but that also proved fruitless.Once I select the dropdown I am attempting to select the option S2 2015.

谢谢您的帮助!

我把time.sleep设为了,因为我认为网站可能没有在该时间之前完全加载,因此正在尝试选择下拉菜单.

I put in the time.sleep because I thought perhaps the website wasn't fully loaded by the time is was trying to select the drop down.

推荐答案

所选元素位于iframe内切换到它:

driver.switch_to.frame("cSearch")

dropdown = driver.find_element_by_id('pg0_V_ddlTerm')
select_box = Select(dropdown)
select_box.select_by_value('2015;S2')

这篇关于使用Selenium和python选择下拉菜单时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:20