无法在 Python 中使用 Selenium 访问下拉选择 [英] Can't access dropdown select using Selenium in Python

查看:26
本文介绍了无法在 Python 中使用 Selenium 访问下拉选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在 Python 中使用 Selenium 的新手,我正在尝试访问 Barclays Live 网站上的索引数据.登录并加载页面后,我尝试从页面的下拉列表中选择Custom1".与列表关联的 HTML 代码中的选择对象如下所示:

I'm new to using Selenium in Python and I'm trying to access index data on Barclays Live's website. Once I login and the page loads, I'm trying to select 'Custom1' from a dropdown in the page. The select object in the HTML code associated with the list looks like this:

<select name="customViewId" class="formtext" onchange="submitFromSelect('username');return false;">
    <option value="">&nbsp;</option>
    <option value="Favorite Indices">Favorite Indices</option>
    <option value="Custom1">Custom1</option>
    <option value="CB group">CB group</option>
    <option value="Kevin Favorites">Kevin Favorites</option>
    <option value="LB Gov/Cdt intermediate">LB Gov/Cdt intermediate</option>
</select>

这是我尝试访问此对象之前的代码:

This is my code up until I try to access this object:

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

#Get chrome driver and connect to Barclays live site
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
browser.get('https://live.barcap.com/')

#Locate username box and enter username
username = browser.find_element_by_name("user")
username.send_keys("username")

#Locate password box and send password
password = browser.find_element_by_name("password")
password.send_keys("password")

#Click login button
login = browser.find_element_by_id("submit")
login.click()

#Open page where you can select indices
browser.get("https://live.barcap.com/BC/barcaplive?menuCode=MENU_IDX_1061")

我尝试了许多我发现的建议解决方案,通常会出现错误无法定位元素:",然后是我尝试使用的任何方法访问选择对象.我似乎无法通过名称、xpath 或使用 Select() 函数访问它.我试过在代码中放置等待时间,以防元素尚未加载,但没有运气.一些我希望工作但不工作的例子:

I've tried a number of proposed solutions that I've found, usually with the error "Unable to locate element: " followed by whatever method I tried to access the select object with. I don't seem to be able to access it by name, xpath, or by using the Select() function. I've tried putting wait time in the code in case the element hadn't loaded yet, and no luck. Some examples of things I would expect to work, but don't are:

select_box = browser.find_element_by_name("customViewId")
select_box = browser.find_element_by_xpath("//select[option[@value='Custom1']]"

我的背景不是编程,如果这是一个愚蠢的问题,请放轻松.在此先感谢您的帮助.

My background isn't in programming, go easy on me if this is a stupid question. Thanks in advance for the help.

推荐答案

select 元素确实位于 iframe 中.

The select element is indeed located in an iframe.

这意味着您应该切换到框架的上下文然后才找到元素:

This means that you should switch into the context of the frame and only then find the element:

browser.switch_to.frame("frame_name_or_id")
select_box = browser.find_element_by_name("customViewId")

如果您需要从框架的上下文中返回,请使用:

If you need to get back from the context of the frame, use:

browser.switch_to.default_content()

至于操作选择框部分,有一个更好的方法——使用选择:

As for the manipulating the select box part, there is a better way - use the Select class:

from selenium.webdriver.support.select import Select

select_box = Select(browser.find_element_by_name("customViewId"))
select_box.select_by_visible_text("CB group")

这篇关于无法在 Python 中使用 Selenium 访问下拉选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆