Selenium can not find an element [duplicate] - python

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

Related

Python selenium I can't acess to a linked page on a page with iframe

I want to go from this page to this https://resultats.ffbb.com/organisation/b5e6211d5970.html to this page https://resultats.ffbb.com/championnat/b5e6211f621a.html?r=200000002810394&d=200000002911791&p=2 by clicking on 'Régional féminin U15'.
I have tried many solutions but the best I had, is not working systematically.
Please coul you help me?
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import selenium.webdriver.support.ui as ui
import time
driver = webdriver.Firefox()
driver.get("https://resultats.ffbb.com/organisation/b5e6211d5970.html")
driver.switch_to.frame("idIframeChampionnat")
#sign_in = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/pre/span[93]'))).click();
button = driver.find_element_by_partial_link_text(u"minin U15")
button.click()```
To click() on the link with text as Régional féminin U15 as the elements are 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://resultats.ffbb.com/organisation/b5e6211d5970.html")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#idIframeChampionnat")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Régional féminin U15"))).click()
Using XPATH:
driver.get("https://resultats.ffbb.com/organisation/b5e6211d5970.html")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='idIframeChampionnat']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Régional féminin U15"))).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:
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
Try
driver.switch_to.frame("idIframeChampionnat")
button = driver.find_element_by_xpath("//a[contains(text(), 'minin U15')]")
button.click()
or
driver.switch_to.frame("idIframeChampionnat")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'minin U15')]"))).click()
This is my code example in groovy (sorry, haven't python env), pretty similar to yours, and it works perfectly 20 times in a loop even without timeouts.
#Test(invocationCount = 20)
void test() {
driver.get('https://resultats.ffbb.com/organisation/b5e6211d5970.html')
driver.switchTo().frame(
driver.findElement(By.xpath("//iframe[#id='idIframeChampionnat']"))
)
driver.findElement(By.xpath("//a[contains(text(), 'minin U15')]")).click()
driver.switchTo().defaultContent()
assert driver.getCurrentUrl() ==
'https://resultats.ffbb.com/championnat/b5e6211f621a.html?r=200000002810394&d=200000002911791&p=2'
}
So, I have no ideas. Maybe just add driver.switch_to.default_content() after click?

Selenium selenium.common.exceptions.NoSuchElementException error sending text to an element within iframe

im new to python and recently got into selenium , i made small projects for linkedin or twitter with it from a tutorial but now i wanted to do smth for my work(finance) and my problem is:
On this website: https://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui
When i try to find a element by any selector(name xpath css selectors etc) it tells me there is no such element
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
from selenium.common.exceptions import NoSuchElementException
URL = 'https://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui'
s = Service('My chromedriver path')
driver = webdriver.Chrome(service = s)
driver.get(URL)
driver.maximize_window()
time.sleep(3)
cui_entry = driver.find_element(By.CSS_SELECTOR,'.col-sm-4 p input')
cui_entry.send_keys('23484xxx')
What i want it to do is to write this code where it says Introduceti codul unic de identificare (numeric): but it seems like im doing something wrong
To send a character sequence to the Enter the unique identification code (numeric) field as the elements are 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://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#_com_liferay_iframe_web_portlet_IFramePortlet_INSTANCE_AAFALwmoH3eD_iframe")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='cod']"))).send_keys("23484")
Using XPATH:
driver.get("https://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='_com_liferay_iframe_web_portlet_IFramePortlet_INSTANCE_AAFALwmoH3eD_iframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='cod']"))).send_keys("23484")
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:
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

"Accept Cookies" popup with Selenium in Python using explicit wait

I have been trying to webscrape some information from a webside with Selenium in Python. I load the webside, but I can't accept the cookies, which is necessary to continue. I have tried to use explicitly wait as I suspected that the problem was that the webside loaded before the "accept" bottom becomes clickable.
My code is:
import os
import until as until
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
os.environ['PATH'] += r"C:\Users\BackUp HDL\AppData\Local\Programs"
driver = webdriver.Chrome()
driver.get("https://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="notice"]/div[3]/button[2]')))
driver.find_element_by_xpath('//*[#id="notice"]/div[3]/button[2]').click()
Whenever I try to run the code I get an error. Can someone please help me out?
The element Accepter 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://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Accepter']"))).click()
Using XPATH:
driver.get("https://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#title='Accepter']"))).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

Selenium Python: Trying to get an iframe within an iframe

I'm trying to switch the Selenium 'focus' to the contents inside of an iframe that's inside of another iframe. My selenium test below seems to work so far. However, there is another iframe within that iframe that I want to get- but I'm not sure now.
Here is the markup (second iframe highlighted):
This is my test so far (which works- just not sure how to get the second iframe within the first iframe):
import unittest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from .functional_test import FunctionalTest
class MyTest(FunctionalTest):
URL = '/datastory/my-datastory/'
#classmethod
def setUpClass(cls):
"""Set up method."""
super().setUpClass()
cls.URL = cls.format_url(cls.URL)
cls.login_user(cls)
def test_iframe(self):
container = self.browser.find_element_by_id('visual-31')
carousel = container.find_element_by_css_selector('.col-sm-12:nth-child(2) div.visual #classics-50-carousel')
wait = WebDriverWait(self.browser, 60)
wait.until(ec.visibility_of(carousel))
page_source = carousel.get_attribute("src");
self.assertEqual(
page_source,
'https://observablehq.com/embed/#ddsg/paramount-databyte-visuals?cells=classics50yrs'
)
# switch Selenium focus to the iframe
iframe = self.browser.switch_to.frame(carousel);
#NOW HOW DO I GET THE OTHER IFRAME??
# switch back
self.browser.switch_to.default_content()
To switch 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.
Then driver.find_element()
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://xyz/datastory/my-datastory/')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.content[src='https://observablehq.com/embed/#ddsg/paramount-databyte-visuals?cells=classics50yrs']")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.content[src*='observableusercontent']")))
element = driver.find_element(By.ID, "id")
Using XPATH:
driver.get('https://xyz/datastory/my-datastory/')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='content' and #src='https://observablehq.com/embed/#ddsg/paramount-databyte-visuals?cells=classics50yrs']")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'observableusercontent')]")))
element = driver.find_element(By.ID, "id")
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

Unable to locate element within an iframe through Selenium

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

Categories