2

The problem: trying to click on a drop-down within an iframe using Chrome driver and Selenium with Python.

Hi all. A user kindly helped me with a newbie query yesterday. I was unable to click on a link within a url and this was because I had to switch into an iframe. This part of the code now works and I navigate to a drop-down menu for which I wish to make a selection.

I've tried accessing this element through amending my code but receive the traceback that it is unable to locate the element. I am trying to change the value of the drop-down to 'Aldershot' using Select, finding the element by name and visible text. Any advice greatly appreciated.

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

#utilise chrome driver to open specified webpage
driver = webdriver.Chrome("/Users/philthomas/Desktop/web/chromedriver")
driver.maximize_window()
driver.get("http:enfa.co.uk")

#switch to specific iframe and click on 'clubs' button on left hand menu
driver.switch_to.frame(2);
ClubsLink=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,
"//span[contains(text(),'Clubs')]")))
ClubsLink.click()

#find drop-down menu and choose 'Aldershot'
select_box = Select(driver.find_element_by_name("team"))
select_box.select_by_visible_text("Aldershot")

Traceback:

Traceback

HTML:

HTML from url

1 Answer 1

3

The reason you are getting error because the select dropdown present inside an iframe. You need to switch to iframe first inorder to select the element.

Induce WebDriverWait and frame_to_be_available_and_switch_to_it()

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.select import Select

#utilise chrome driver to open specified webpage
driver = webdriver.Chrome("/Users/philthomas/Desktop/web/chromedriver")

driver.maximize_window()
driver.get("http:enfa.co.uk")

#switch to specific iframe and click on 'clubs' button on left hand menu
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"left")))
ClubsLink=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,
"//span[contains(text(),'Clubs')]")))
ClubsLink.click()

#return from iframe
driver.switch_to.default_content()
#Switch to another iframe
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"main")))

#find drop-down menu and choose 'Aldershot'
teamselect=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"team")))
select_box = Select(teamselect)
select_box.select_by_visible_text("Aldershot")

Browser snapshot:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much for this reply, works fantastically. Is there a specific course/tutorial you followed to learn this particular approach or just a case of working through the specific Selenium docs? Am keen to improve and learn more as still very new to programming. Many thanks
No such specific document.Just follow selenium official document you will get most of the things.

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.