我已经在这个问题上挣扎了好几天,并尝试了各种实现,在堆栈溢出建议中进行了搜索,取得了不同程度的成功。在
我试图使用Python和Selenium自动从下拉框中选择一个选项。我在更简单的网站实现上取得了成功,比如Facebook为DOB选择日期、年份、月份
chosen_homepage = "https://www.facebook.com/"
browser = webdriver.Chrome()
browser.maximize_window()
browser.get(chosen_homepage)
the_month_id = "month"
the_day_id = "day"
the_year_id = "year"
element5 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, the_month_id)))
from selenium.webdriver.support.select import Select
the_month = Select(browser.find_element_by_id(the_month_id))
the_month.select_by_visible_text("Mar")
the_day = Select(browser.find_element_by_id(the_day_id))
the_day.select_by_visible_text("15")
the_year = Select(browser.find_element_by_id(the_year_id))
the_year.select_by_visible_text("1985")
然而,这个网站被证明是更棘手的。我正试图登录here
从下拉列表中自动选择一个区域,例如“AK”,在本例中是选项3。我可以选择(单击)下拉框并在GUI上显示其值选项,方法是:
^{pr2}$在这里它变得更加棘手,我尝试了几种替代方法来选择列表中的一个选项,例如使用sendkeys()使用值,甚至使用向下箭头操作(单步执行列表),但都没有成功。在
所以我试着把这个问题再分解一下。似乎有几个嵌套的对象调用,我试图列出从下拉列表中选择时触发的主要组件,即reOptionCheck=>;jfpw select=wrapper=>;jfpw select wrapper jfpw select list container
<div id="rpOptionCheck">
<div class="jfpw-select-wrapper">
<div class="jfpw-select-wrapper jfpw-select-list-container"
<ul class="ui-selectmenu-menu ui-widget ui-widget-content ui-selectmenu-menu-dropdown ui-corner-bottom jfpw-select ui-selectmenu-open" aria-hidden="false" role="listbox" aria-labelledby="RegionalPricingLocation-snapshot-button" id="RegionalPricingLocation-snapshot-menu" aria-activedescendant="RegionalPricingLocation-snapshot-menu-option-4" tabindex="0" style="width: 258px; max-height: 160px; float: left; overflow: hidden; outline: none; z-index: 1002; top: 0px; left: 0px; position: relative; border-style: none;"><li role="option" tabindex="-1" aria-selected="false" id="RegionalPricingLocation-snapshot-menu-option-0" class=""><span class="ui-selectmenu-item-header">Select a State</span></li><li role="option" tabindex="-1" aria-selected="false" id="RegionalPricingLocation-snapshot-menu-option-1" class=""><span class="ui-selectmenu-item-header">AA</span></li><li role="option" tabindex="-1" aria-selected="false" id="RegionalPricingLocation-snapshot-menu-option-2" class=""><span class="ui-selectmenu-item-header">AE</span></li><li role="option" tabindex="-1" aria-selected="false" id="RegionalPricingLocation-snapshot-menu-option-3" class=""><span class="ui-selectmenu-item-header">AK</span></li><li role="option" tabindex="-1" aria-selected="true" id="RegionalPricingLocation-snapshot-menu-option-4" class="ui-selectmenu-item-selected"><span class="ui-selectmenu-item-header">AL</span></li><li role="option" tabindex="-1" aria-selected="false" id="RegionalPricingLocation-snapshot-menu-option-5"><span class="ui-selectmenu-item-header">AP</span></li><li role="option" tabindex="-1" aria-selected="false"
...
我已经设法在编写这段代码的元素之间循环
region_option3 = "RegionalPricingLocation-snapshot-menu" # Called in last component listed above
html_list = browser.find_element_by_id(region_option3)
items = html_list.find_elements_by_tag_name("li") # Collects all List Elements
# items.get(1).click()
option_no3 = "RegionalPricingLocation-snapshot-menu-option-3" # As a test look for 3rd item "AK"
for item in items:
print(item.get_attribute("value"), item.get_attribute("text"), item.get_attribute("id"))
if item.get_attribute("id") == option_no3:
print("Found 3rd Item - Option 3")
break
这段代码的输出是: Output Results
如您所见,输出与列表中的实际值不匹配,更像位置指示器,例如“regional pricinglocation-snapshot-menu-option-3”,我怀疑我是从错误的级别进入的,但是,他们试图以各种方式在更高的对象级别设置焦点和选择,所有这些似乎都会抛出异常。我可以看到aria selected=“true”对于所选的选项,在本例中,它适用于第三个列表项(RegionalPricingLocation-snapshot-menu-option-3),但不确定如何设置它,因为它看起来像是传递的输入。在
回到顶层(rpOptionCheck)并查看生成的HTML,我可以看到屏幕上的Select选项内的列表值,这让我相信我可以使用与上面Facebook示例相同的方法使用Select()方法。在
我现在正忙得不可开交,不知道该怎么办。我是Python和Selenium的新手,这将是我的第一篇文章。希望它的格式是正确的。非常乐意按要求修改以符合标准。在
提前感谢你的任何想法或文章的建议,可以帮助我阐明我缺少什么/我错在哪里。在
更新1:
我发现了这个题目,看起来和第二期相似。使用非标准Select实现的类调用
Selenium, Selecting an element inside of a <span>
所以试过了
# <div class="jfpw-select-wrapper jfpw-select-list-container" Note: sits above ul class
# <ul class="ui-selectmenu ui-widget ui-state-default jfpw-select ui-selectmenu-dropdown ui-corner-all
jfpw_select = "jfpw-select"
find_call = browser.find_element_by_class_name(jfpw_select)
# find_call.send_keys('AK')
find_call.send_keys(Keys.DOWN)
#Error Message "selenium.common.exceptions.ElementNotVisibleException: Message: element not visible"
这与google的例子有点不同,因为我可以计算出两个类调用(我认为)。。。在
目前没有回答
相关问题 更多 >
编程相关推荐