Unable to locate element within an iframe through Selenium - python

I'm new to Selenium. I'm trying to write a Python script that will log in to a particular form. The form is located at http://www.tvta.ca/securedContent
The code I'm running is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://tvta.ca/securedContent/")
elem = driver.find_element_by_name("txtUserName")
elem.clear()
elem.send_keys("<<my email>>")
I get an error that says:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="txtUserName"]
I'm not sure what I'm missing here? I've checked the source of the webpage the login field is definitely named txtUserName.

You need to switch to the frame to write text in the textbox, try to check syntax once as I have less in good in Python
framLogin= driver.find_element_by_id("membeeLoginIF")
driver.switch_to.frame(framLogin)
EmailTxt = driver.find_element_by_name("txtUserName");
EmailTxt.send_Keys("Test#gmail.com");
Same in Java
WebElement framLogin= driver.findElement(By.id("membeeLoginIF"));
driver.switchTo().frame(framLogin);
WebElement EmailTxt = driver.findElement(By.name("txtUserName"));
EmailTxt.sendKeys("Test#gmail.com");

That site requires third-party cookies to be enabled; without them, the login form does not load. It's likely you have your browser configured that way but the defaults for webdriver.Firefox do not.
To see what Selenium is actually seeing, dump driver.page_source and/or take a screenshot with driver.save_screenshot(...).

The desired element is within an <iframe>. So as per best practices 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 and you can use the following Locator Strategies:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"membeeLoginIF")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.textboxWaterMark#txtUserName"))).send_keys("Jeff")
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

Related

How to change between iframes in Selenium and Python

I was trying to understand how to change between iframes in selenium webdriver, but I could not figure it out.
In the image ahead, is the iframe that I want to switch to:
Snapshot:
//iframe[#class='viewer pbi-frame']
Should be a simple xpath using driver.switch_to.frame()
Or
//iframe[#title='Power BI Report Viewer']
In order to switch to the iframe you can use code like this:
driver.switch_to.frame(driver.find_element_by_css_selector("//iframe[#title='PowerBi Report Viewer']"))
When finished working inside the iframe you will have to switch back to the default content with
driver.switch_to.default_content()
The website is Power BI based, so to switch within 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[title='PowerBi Report Viewer']")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='PowerBi Report Viewer']")))
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
Reference
You can find a couple of relevant discussions in:
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

Invalid argument: 'using' must be a string extracting image source within an iframe

I'm new about using python and selenium and I'm trying to get the image source from a website. I'm actually finding elements by the tag name, but I don't know if it's the right way.
Anyway it gives me an error:
InvalidArgumentException: Message: invalid argument: 'using' must be a string
Under this there's the code I wrote
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
PATH = Service("/Users/fscozano/documenti/chromedriver-2.exe")
print("setup")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://apod.nasa.gov/apod/random_apod.html")
tag = driver.find_element((By.TAG_NAME, "b")).getText()
print(tag)
driver.quit()
The error is in:
tag = driver.find_element((By.TAG_NAME, "b")).getText()
You can personally inspect the website because I really don't know how to solve this
Every type of help is appreciated :)
The image source 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 Locator Strategies:
driver.get("https://apod.nasa.gov/apod/random_apod.html")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://apod.nasa.gov/apod']")))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1[normalize-space()='Astronomy Picture of the Day']//following::p[2]//a/img"))).get_attribute("src"))
Console Output:
https://apod.nasa.gov/apod/image/2111/MACSJ0138_Hubble_1080.jpg
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
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

Selenium can not find an element [duplicate]

This question already has answers here:
Ways to deal with #document under iframe
(2 answers)
Closed 2 years ago.
Actually my code is simple and the task too, but somehow i get this Error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"TLoginBox"}
I tried any of the find element and find element by options but none did work.
All i found out is that on the website where the login button is, that the button itself somehow is a whole HTML with head, body,...
T-online Email Link
That's my code:
from selenium.webdriver.common.by import By
from time import sleep
from selenium import webdriver
browser = webdriver.Chrome('F:/trzttrtz/chromedriver_win32/chromedriver')
browser.get('https://www.t-online.de/themen/e-mail')
sleep(5)
login = browser.find_element(By.ID, value="TLoginBox")
login.click()
I am new to programming, so any help means a lot.
Thanks in advance.
The element cannot be found because it is inside another frame, try the following:
from selenium.webdriver.common.by import By
from time import sleep
from selenium import webdriver
browser = webdriver.Chrome('F:/trzttrtz/chromedriver_win32/chromedriver')
browser.get('https://www.t-online.de/themen/e-mail')
frame = browser.find_element(By.CLASS_NAME, 'TlgnBoxIfr')
browser.switch_to_frame(frame)
login = browser.find_element(By.CLASS_NAME, value='TlogBtn')
login.click()
If you want to go back to the main frame use:
browser.switch_to().default_content()
The element with the text as E-Mail Login 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:
Using CSS_SELECTOR:
driver.get('https://www.t-online.de/themen/e-mail')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.TlgnBoxIfr")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.TlogBtn>span.mailicn>img"))).click()
Using XPATH:
driver.get('https://www.t-online.de/themen/e-mail')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='TlgnBoxIfr']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='TlogBtn']/span[#class='mailicn']/img"))).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

Unable to type within username field within ProtonMail signup page using Selenium and Python

Hi I was trying to type the username field using Selenium and Python for the website https://mail.protonmail.com/create/new?language=en.
From the developer tool, I am able to inspect the item using CSSSelector/Xpath or other way. But when I am running the pthon script its not working. Screenshot attached:
My code is like the following:
BASE_URL = 'https://mail.protonmail.com/create/new?language=en'
driver = webdriver.Chrome(executable_path='./drivers/chromedriver')
driver.get(BASE_URL)
river.find_element_by_xpath('//*[#id="username"]').send_keys('someStringValue')
And after executing the following code, geetting the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="username"]"}
(Session info: chrome=83.0.4103.97)
Any suggestion?
The Email Address field 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:
Using CSS_SELECTOR:
driver.get('https://mail.protonmail.com/create/new?language=en')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"div.usernameWrap iframe[title='Registration form']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input#username"))).send_keys("FunnyBoss")
Using XPATH:
driver.get("https://mail.protonmail.com/create/new?language=en")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//div[#class='usernameWrap']//iframe[#title='Registration form']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='input' and #id='username']"))).send_keys("FunnyBoss")
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 relevant discussion in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
Your xpath is OK, but the forms are inside an iframe.
So you need to switch to the iframe first:
driver.switchTo().frame(n);
Edit: If you read the TOS, you will see
This Service is provided exclusively to persons. Accounts registered by “bots” or automated methods are not authorized and will be terminated.

Send keys to HTML element using Selenium

I am writing an automation script for sports betting in Python using Selenium. I am stuck at a point where Selenium is unable to click or send keys to the specific HTML element highlighted in the following screenshot (https://i.stack.imgur.com/NbljY.png).
Here is what I have tried:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.bet365.com")
### Some code here to navigate to a particular match
driver.switch_to.frame(bet_driver.find_element_by_tag_name("iframe"))
elem = driver.find_element_by_class_name("bs-Stake")
elem.click()
elem.send_keys("100")
This returns the following error:
ElementNotInteractableException: Element <div class="bs-Stake"> is not reachable by keyboard
If I try instead
elem = driver.find_element_by_class_name("stk bs-Stake_TextBox")
I get the error:
NoSuchElementException: Unable to locate element: .stk bs-Stake_TextBox
I would appreciate help navigating to the HTML element, clicking and sending keys to it, using any method available in Selenium.
Try to sendkeys with webdriver wait so element would be able to interact.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 50).until(EC.visibility_of_element_located((CSS_SELECTOR, ".stk.bs-Stake_TextBox")))
element.send_keys("100")
Although CSS styles are inherited... the class designations are not. Your css selector has three separate classes in it, but your element sample only has one.
Selenium is designed to mimic the same actions that a person can perform... the error suggests that the field is not available for interaction.... find out why.

Categories