Switch to an iframe through Selenium and python - python

How would I switch to this iframe in selenium knowing only
<iframe name="Dialogue Window">

You can use an XPath to locate the <iframe>:
iframe = driver.find_element_by_xpath("//iframe[#name='Dialogue Window']")
Then switch_to the <iframe>:
driver.switch_to.frame(iframe)
Here's how to switch back to the default content (out of the <iframe>):
driver.switch_to.default_content()

As per the HTML of the <iframe> element, it has the name attribute set as Dialogue Window. So to switch within the <iframe> you need to use the switch_to() method and you can use either of the following approaches:
Using the name attribute of the <iframe> node as follows:
# driver.switch_to.frame(‘frame_name’)
driver.switch_to.frame("Dialogue Window")
Using the <iframe> WebElement identified through name attribute as follows:
driver.switch_to.frame(driver.find_element_by_name('Dialogue Window'))
Using the <iframe> WebElement identified through css-selectors as follows:
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[name='Dialogue Window']"))
Using the <iframe> WebElement identified through xpath as follows:
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[#name='Dialogue Window']"))
Ideally, you should induce WebDriverWait inconjunction with expected_conditions as frame_to_be_available_and_switch_to_it() for the desired frame as follows:
Using NAME:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"Dialogue Window")))
Using CSS_SELECTOR:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='Dialogue Window']")))
Using XPATH:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#name='Dialogue Window']")))
Switching back from frame
To switch back to the Parent Frame you can use the following line of code:
driver.switch_to.parent_frame()
To switch back to the Top Level Browsing Context / Top Window you can use the following line of code:
driver.switch_to.default_content()
tl; dr
Ways to deal with #document under iframe

I have resolved this issue in Python-Selenium. Please use the below code:
srtHandle = driver.window_handles
driver.switch_to_window(srtHandle[0])
Then switch to the frame in which the element is located.

Related

How to find the element using Selenium

I have an element that clears a query after the query is made, but I can't get selenium to locate it.
Here is the xpath for the element:
/html/body/main/div/section[1]/div/a[2]
This is the html for the element:
<a onclick="onClearClicked()" class="btn btn--secondary btn--sm">clear</a>
Things that I have tried:
Finding by using the whole xpath, css selector, class name, text
Using css to find all buttons on the page and iterating over them until I find the right one (it doesn't find any buttons with text)
buttons = mydriver.find_elements_by_css_selector("button")
for button in buttons:
print(button.text)
if button.text.strip() == "clear":
button.click()```
Exiting the iframe I was in before and using the full xpath
driver.switch_to().defaultContent()
I have a work around that involves quitting the driver and reopening it for every query, but this would involve logging in and navigating to the right page every time and I'd much rather be able to just use the clear button.
Your html doesn't have any "button" element; it's an "a" (hyperlink) element. So, you should do:-
buttons = mydriver.find_elements_by_css_selector("a")
Using full xpath not a good way , so try the following code:
buttons =driver.find_elements_by_css_selector("[class='btn btn--secondary btn--sm']")
for button in buttons:
print(button.text)
if button.text.strip() == "clear":
button.click()
driver.find_element(By.XPATH,"//a[.='clear']").click()
If you want to click the element with the text clear you can just look for an a tag with that text.
The element is a <a> tag which have the innerText as clear
<a onclick="onClearClicked()" class="btn btn--secondary btn--sm">clear</a>
Solution
To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "clear"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn--secondary.btn--sm[onclick^='onClearClicked']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn--secondary btn--sm' and starts-with(#onclick, 'onClearClicked')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to identify the iframe using Selenium?

HTML:
<iframe allowpaymentrequest="true" allowtransparency="true" src="https://shopify.wintopay.com/
cd_frame_id_="ca9e4ad6a1559de159faff5c1f563d59"
name="WinCCPay"
id="win-cc-pay-frame"
I'm trying to input text in a CC field. Apparently its in an iframe I picked the last one in the HTML and tried to select it from the identifiers above but I keep getting the element couldn't be found
iframe= wd.find_element_by_id("win-cc-pay-frame")
wd.switch_to.frame(iframe)
The frame is currently being shown in the browser so no need for implicit wait.
To identify the <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#win-cc-pay-frame[name='WinCCPay'][src^='https://shopify.wintopay.com']")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='win-cc-pay-frame' and #name='WinCCPay'][starts-with(#src, 'https://shopify.wintopay.com')]")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
The problem can be that the name and id of the element are dynamic and change for each unique checkout window? Can you check if adding class attribute at iframe tag and find element by this attribute?
It must be similar to:
iframe = wd.find_element_by_class_name('card-pay-iframe')
wd.switch_to.frame(iframe)
...
wd.switch_to.default_content()
good coding! ¯_(ツ)_/¯

How to click the Continue button within https://in.indeed.com/ website using selenium in python?

I am trying to write a code that is able to auto apply on job openings on indeed.com. I have managed to reach the last stage, however, the final click on the application form is giving me a lot of trouble. Please refer the page as below
Once logged in to my profile, I go to the relevant search page, click on the listing I am interested in and then on the final page (shown above) I am trying to click on the continue button using xpath. For the previous step (also a page on the same form), the form had multiple iframes which I was able to switch successfully. Now for the current page, I am stuck as the click function does not do anything. I have written the following code:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
driver.find_element_by_xpath('//*[#id="apply-button-container"]/div[1]/span[1]').click()
time.sleep(5)
frame_1 = driver.find_element_by_css_selector('iframe[title="Job application form container"')
driver.switch_to.frame(frame_1)
frame_2 = driver.find_element_by_css_selector('iframe[title="Job application form"]')
driver.switch_to.frame(frame_2)
continue_btn = driver.find_element_by_css_selector('#form-action-continue')
continue_btn.click()
driver.find_element_by_xpath('//*[#id="form-action-continue"]').click()
I have tried switching the iframes again for this step but nothing happens. Even the .click() function does not do anything.
Will appreciate some help on this.
The element Continue is within nested <iframe> elements so you have to:
Induce WebDriverWait for the parent frame to be available and switch to it.
Induce WebDriverWait for the child frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.indeed-apply-button-label"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='indeedapply-modal-preload']")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Job application form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#form-action-continue"))).click()
Using XPATH:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='indeed-apply-button-label']"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#id, 'indeedapply-modal-preload')]")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='Job application form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='form-action-continue']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Select a textbox with Python Selenium, changing frame

Warning: Complete Novice
I'm trying to select the text box for the 'Login ID' on the website:
https://www.schwab.com/public/schwab/nn/login/login.html&lang=en
I'm using Python 3.7 and Selenium. I've tried to inspect the textbox element to find the CSS Selector. This causes an error. I know I need to switch my 'frame', but I can't read the website back end coding well enough to find the right frame name.
from selenium import webdriver
browser = webdriver.Chrome(executable_path=driver_path, chrome_options=option)
Login_Id_TextBox = browser.find_element_by_id('LoginId')
Can anyone help me find the right frame or direct me to a good source to learning more about them?
To send a character sequence with in the text box for the Login ID on the website https://www.schwab.com/public/schwab/nn/login/login.html&lang=en 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 either of the following Locator Strategies:
CSS_SELECTOR:
driver.get('https://www.schwab.com/public/schwab/nn/login/login.html&lang=en')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#loginIframe")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control#LoginId"))).send_keys("averagejoe1080")
XPATH:
driver.get('https://www.schwab.com/public/schwab/nn/login/login.html&lang=en')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='loginIframe']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='form-control' and #id='LoginId']"))).send_keys("averagejoe1080")
Here you can find a relevant discussion on Ways to deal with #document under iframe
Here is the simple code for your requirement, I test this successfully.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.schwab.com/public/schwab/nn/login/login.html&lang=en')
driver.switch_to_frame("loginIframe")
Login_Id_TextBox = driver.find_element_by_id('LoginId')

How to find element using Selenium and Chromedriver

I am new to Selenium. I am using Selenium 3.141 from anaconda on OSX. My Chrome version is 71.0 and Chromedriver version is 2.45. My goal is to use Selenium to click on a button with "Accept" on it on a webpage. I am able to instantiate the webdriver object using the executable and use the same to load the page in question. I then have a wait for 20 seconds. It's the next bit that fails. Code is unable to find the element that has to be clicked on. Attached are two images of elements associated with the button at various levels and sublevels with highlighted parts from inspection. I have tried variants such as
accept_button = driver.find_element_by_class_name('flex-x.static.h- center.v-center.bt-button.filled')
and
accept_button = driver.find_element_by_class_name('Accept')
and a few others to no avail. Please help.
Button frame:
Accept rectangle:
The desired element is an Angular element so to locate/click you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
accept_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "bt-button.Aceept.ng-isolate-scope[mutable-label='prelogin.translations.Accept']")))
Using XPATH:
accept_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//bt-button[#class='Aceept ng-isolate-scope focused' and #mutable-label='prelogin.translations.Accept']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
did you see bt-button.Accept.ng-isolate-scope.focused you can use it for selecting using css selector
accept_button = driver.find_element_by_css_selector('bt-button.Accept.ng-isolate-scope.focused')
# or
accept_button = driver.find_element_by_css_selector('bt-button.Accept')
I would suggest you to check if the element you trying to interact with is in iframe.
To identify this you can: Right click on the element, If you find the option like 'This Frame' then it is an iframe; or you can just search in page source for iframe tag to identify.
you can refer to this question for further solution if indded your element is part of iframe: Select iframe using Python + Selenium

Categories

Resources