Unable to click on dropdown within iframe - Selenium Python - python

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:
HTML:

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:

Related

Retrieve class schedule changes from website using python and selenium No Such Element error, ID, XPATH, and more

My school has a system that tells us if our schedule has any changes.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.select import Select
url = "https://www.alliancetlv.com/עדכוני-מערכת"
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(5.0)
examButton = driver.find_element(By.ID, 'TimeTableView1_btnChanges')
im trying to find an element, and later click it using selenium. every time i try to find literally anything it returns No Such Element error. I tried by ID, class name, name, and more.
this is the website: https://www.alliancetlv.com/עדכוני-מערכת
and im trying to click one of the tabs called "changes/שינויים"
my end goal is to click the dropdown to the side, select a class, click the changes tab, then get all the data inside of it, then maybe format it.
You can not seek the element because it is layed inside the iframe. So, you have to:
find iframe
switch to it
find your element
click on it
frame = driver.find_element(By.XPATH, value="//iframe[#title='Embedded Content']")
driver.switch_to.frame(frame)
elem = driver.find_element(By.XPATH, value="//a[contains(#id,'TimeTableView1_btnChanges')]")
elem.click()
See docs...

Click sub-element button when it appears after clicking another button with Python Selenium?

I'm having trouble with the Selenium webdriver's click() feature in Python when I'm trying to click on buttons that only appear after you click on a parent button.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
url = "https://law.lexmachina.com/cases/?pending-from=2000-01-01&pending-to=2000-02-01&filters=true&tab=summary&view=analytics&cols=475"
driver.get(url)
driver = webdriver.Chrome()
elem0 = driver.find_element_by_id('export-icon-container') # this works
all_children_by_css = elem0.find_elements_by_css_selector("*") # this works, but doesn't click on the sub-button (XLS) one successfully when I run below...
all_children_by_css[0].click() # this just makes the parent button's little window appear and disappear, the same as elem0.click() does.
>>> all_children_by_css[0] # this is the webElement that I thought was for the XLS button
<selenium.webdriver.remote.webelement.WebElement (session="6b4a559408fa4d512f8596759d81eaf7",
element="d83be2ca-c879-4706-85ef-db7120d345a3")>
Basically, I want to export the XLS file via the Webdriver, so that later on I can do this on a loop with URLs of filters of the data.
I included annotated screenshots below, detailing the buttons I'm trying to click & the inspected code associated with them.
I would recommend to see if there an API on the page to export directly. if not, You would probably need dynamic wait. you can try something like below.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
url = "https://law.lexmachina.com/cases/?pending-from=2000-01-01&pending-to=2000-02-01&filters=true&tab=summary&view=analytics&cols=475"
driver.get(url)
#Click on Export Icon
elem0 = driver.find_element_by_id('export-icon-container').click()
#Wait for XLS option to show up
wait = WebDriverWait(driver, 10)
ExportOption= wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#data-action='export-item'][contains(text(),'XLS')]")))
ExportOption.click()

Unable to locate element error through Selenium after copying xPath location

Selenium, cannot locate xPath element
I'm a beginner learning Selenium, trying to build a tiny web scraper. First part is to go to the site and click button "Search By Address". I copied the xPath from Inspecting the element in Google Chrome, however, using Selenium to find the element keeps throwing a "unable to locate" error.
from selenium import webdriver
web_Url = 'http://hcad.org/quick-search/'
driver = webdriver.Firefox(executable_path=r'C:\Users\Jaz\Documents\Python\Modules\geckodriver.exe')
driver.implicitly_wait(10) # this lets webdriver wait 10 seconds for the website to load
driver.get(web_Url)
button = driver.find_element_by_xpath("//*[#id='s_addr']").click()
The element you are trying to find is inside iframe. First, switch to iframe using 'driver.switchTo().frame()' command and then try to find the element.
More details on switching to iframe is discussed in this article.
from selenium import webdriver
web_Url = 'http://hcad.org/quick-search/'
driver = webdriver.Firefox(executable_path=r'C:\Users\Jaz\Documents\Python\Modules\geckodriver.exe')
driver.implicitly_wait(10) # this lets webdriver wait 10 seconds for the website to load
driver.get(web_Url)
element = driver.find_element_by_xpath("//div[#class='c-general']/iframe")
driver.switch_to_frame(element)
driver.find_element_by_xpath("//*[#id='s_addr']").click()
driver.switch_to_default_content()
To click() on the element with text as Search By Address as the the desired element is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://hcad.org/quick-search/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#src='https://public.hcad.org/records/quicksearch.asp']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#s_addr"))).click()
Browser Snapshot:
Here you can find a relevant discussion on Ways to deal with #document under iframe

How to click on an element from the Dropdown menu through Python and Selenium

I'm trying to click on the drop down menu but with no luck .
the menu is activated by javascript .
I tried to click on the link inside the parent div but nothing happens
here is some code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.energisa.com.br/Paginas/login.aspx")
select_element = Select(driver.find_element_by_id('ddlEstado'))
select_element.select_by_value('MG')
# select by visible text
select_element.select_by_visible_text('MG')
As per the your question the website https://www.energisa.com.br/Paginas/login.aspx the dropdown menu is not with in a Select tag, so Select class won't work here.
Once the url is accessed, you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.energisa.com.br/Paginas/login.aspx")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[#class='estado']/div[#class='select2-container' and #id='s2id_ddlEstado']"))).click()
driver.find_element_by_xpath("//ul[#class='select2-results' and #id='select2-results-1']//li/div[normalize-space()='MG']").click()
The dropdown you are trying to click is not actually a SELECT element so you can't use the Select class. The SELECT you are trying to click is just a backing element but it's invisible so you can't interact with it.
To make this work, you will need to click the dropdown element to expose the options and then click the desired option.
driver.find_element_by_css_selector("#s2id_ddlEstado > a").click()
driver.find_element_by_xpath("//ul[#id='select2-results-1']/li[.='MG']").click()
This is untested code, so you may need to add a wait...
The one which appears like a select_list is not a select_list, the purpose of this kind of select_list is, we can write into the text_field to pick up the elements from the huge list, if you type 'M', then all the options which has M will be displayed.
Write the following code, it would work.
WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.ID,"loadingContent")))
driver.find_element_by_id("s2id_ddlEstado").click
driver.find_element_by_xpath("//ul[#id='select2-results-1']//div[text()='MG']").click

Selenium can't locate the element, a search button using xpath

I can't find a solution how this element cannot be found by using a selenium xpath. Other button on other websites always working just fine. Usually what I would normally do is, just open up a page and inspect the element and I would right click it and copy the xpath. Done.
But this website, www.gsc.com.my (a malaysian cinema booking site). Seems not able to find the button. Is it protected by another security layer?
Lets see the code below,
from selenium import webdriver
chromedriver_path = './chromedriver.exe'
driver = webdriver.Chrome(chromedriver_path)
driver.get('https://www.gsc.com.my')
driver.find_element_by_xpath("""//*[#id="btnNext"]""").click()
The error Message:
no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="btnNext"]"}
Button is located inside an iframe, so you need to switch to that frame before clicking the button:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
driver.switch_to.frame('getQuickFilter_ctrl_ifrBox')
wait(driver, 10).until(EC.element_to_be_clickable((By.ID, "btnNext"))).click()
Because there are two elements with id btnNext, you'll have to specify which of them using an index, 1 for the first, 2 for the second.
driver.find_element_by_xpath("""//*[#id="btnNext"][1]""").click()
You can try with this css selector :
div.container input#btnNext
Note that you will need to switch to iframe first , cause the check button is in a iframe.
For switching to iframe :
driver.switch_to.frame(driver.find_element_by_id("getQuickFilter_ctrl_ifrBox"))
and for clicking on check Button
wait = WebDriverWait(driver, 10)
check_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.container input#btnNext")))
check_button.click()

Categories

Resources