3

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.

2
  • Could you check if this select element is located inside an iframe or not? Commented Jul 13, 2016 at 16:02
  • The select element is indeed located in an iframe. Commented Jul 13, 2016 at 16:34

3 Answers 3

1

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")
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

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.
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
0

I also tried various way like above things to solve this problem. Unfortunately, I couldn't solve this problem using above ways. But, My problem has solved using that send click event to action element.

select = browser.find_element_by_xpath("//select[@id='reservation_period']")
for option in select.find_elements_by_xpath(".//option"):
    if option.text == "2 Days":
        option.click()
        break
    select.send_keys(Keys.ARROW_DOWN)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.