Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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

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.

share|improve this question
    
Could you check if this select element is located inside an iframe or not? – alecxe Jul 13 at 16:02
    
The select element is indeed located in an iframe. – kforce Jul 13 at 16:34
up vote 1 down vote accepted

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")
share|improve this answer
    
This worked perfectly. Thanks so much! – kforce Jul 13 at 16:57

One method to alter select elements by sending keys to it.

First locate the select element, then send keys to it. You usually only need the first 1-3 letters to get the proper (unique) result out of element.

select_elem = browser.find_element_by_name("customViewId")
select_elem.send_keys("cu")

Another option would be to send arrowkey strokes or something like that, or to do a .click() on the select, then select the from the dropdown or send keys if sending keys without clicking first doesn't work.

Also, you can also find elements relative to another element, such as:

select_box = browser.find_element_by_name("customViewId")
option = select_box.find_element_by_xpath("/[option[@value='Custom1']]") #example only - untested for this case

Finally, another option would be to execute some JavaScript arbitrarily to change the element using browser.execute_script -- but writing JS code to do that is probably beyond scope here.

YMMV depending on the particular page/method.

share|improve this answer
    
Unfortunately this doesn't work for me. The error is raised in trying to find the select element in the first place. browser.find_element_by_name results in an error. – kforce Jul 13 at 16:37
    
Okay, I see. Your problem was not selecting the options... but rather... was selecting the select element to begin with. I misunderstood and did not know it was in an iFrame – Gator_Python Jul 13 at 17:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.