Unable to locate element within an iframe while the element actually exists - python

No matter how long I wait, it seems selenium can't find the "watch_online" button.
I've tried both by XPath, full XPath, and CSS selector.
I want to get the href link from the "Watch Online" button.
import os
import glob
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
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
# Create local browser cache
browser_data_location = "browser_data"
options = webdriver.ChromeOptions()
options.add_argument(f'user-data-dir={os.getcwd()}/{browser_data_location}')
i_m_not_a_robot_xpath = '//*[#id="landing"]/div[2]/center/img'
generate_link_xpath = '//*[#id="generater"]/img'
click_to_continue = '//*[#id="showlink"]'
get_download_link = '/html/body/section/div/div/div/center/a'
watch_online = '//*[#id="download-hidden"]/a'
with webdriver.Chrome(options=options, ) as driver:
wait = WebDriverWait(driver, 10)
time.sleep(2)
driver.get(
"https://www.rtilinks.com/?82255aba71=RmwzVDZObDFBdDQvay8zRjhiaStoM004Ymd1T201MnBQelJpdW5oK1UxeGFvbFZUY1FEVXMrY0o2UnhqeGxOOFlwN3JlUElad2h0ek9pQ1ZFZndXSG9UTzA1aFpmTEhoanBVUldEYWwwWVU9")
# wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, upload_box_css))).send_keys(file)
wait.until(ec.element_to_be_clickable((By.XPATH, i_m_not_a_robot_xpath))).click()
# time.sleep(1)
wait.until(ec.element_to_be_clickable((By.XPATH, generate_link_xpath))).click()
wait.until(ec.element_to_be_clickable((By.XPATH, click_to_continue))).click()
# original_window = driver.current_window_handle
driver.close()
driver.switch_to.window(driver.window_handles[0])
wait.until(ec.element_to_be_clickable((By.XPATH, get_download_link))).click()
time.sleep(2)
link = driver.find_element(By.XPATH, watch_online)
print(link.get_attribute('href'))

The element Watch Online 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 PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'https://purefiles.in')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Watch Online"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://purefiles.in']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.button.is-success"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='button is-success' and contains(., 'Watch Online')]"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'https://purefiles.in')]")))
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

Related

Selenium python cannot find name or email address element

I am trying to automatically login the website using selenium with python. However, I got error as below.
Traceback (most recent call last):
File "C:\Users\KienThong\Automation\Learning\GmailDemo.py", line 15, in <module>
ID = WebDriverWait(driver, 10).until(
File "C:\Python385\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
My code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.get("https://www.englishforum.com/study/login/")
driver.maximize_window()
try:
ID = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "_xfUid-1-1643985254"))
)
ID.send_keys("dkthong2010#gmail.com")
finally:
driver.quit()
The reason why you are having an error is that the ID that you are using is dynamic and changes everytime the page is loaded. Therefore we need to use a static identifier which we can with name="login"
driver = webdriver.Chrome()
driver.get("https://www.englishforum.com/study/login/")
driver.maximize_window()
try:
ID = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "login"))
)
ID.send_keys("dkthong2010#gmail.com")
finally:
driver.quit()
you dont need to tell the driver to wait till the element is present your can simply its xpath as it will executed only after page loads
so use:
ID = driver.find_element(By.XPATH, "/html/body/div[2]/div/div[4]/div/div[2]/div/div/div/div/form/div[1]/div/dl[1]/dd/input")
ID.send_keys("dkthong2010#gmail.com")
using xpath to locating the element is very easy and easy to locate.
just
1.inspect the element
2.right click on the element
3. copy full xpath
To send a character sequence to the Your name or email address field you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://www.englishforum.com/study/login/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href$='dismiss-notice'] > span.button-text"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='login']"))).send_keys("dkthong2010#gmail.com")
Using XPATH:
driver.get('https://www.englishforum.com/study/login/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href, 'dismiss-notice')]/span[#class='button-text']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='login']"))).send_keys("dkthong2010#gmail.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
Browser Snapshot:

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?

"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

How do I type login credentials using Selenium and Python within kingdoms.com?

I tried a few websites don't have a problem. "baseURL" tried a lot of time.
driver.find the class element
can't get. Anyone can help?
Click this link below and inside the login page ...
which one to use ??
Code trials:
PATH = "D:\chromedriver.exe"
driver = webdriver.Chrome(PATH)
baseURL = "https://www.kingdoms.com/#logout"
driver = get(baseURL)
print(driver.title)
driver.find_element_by_xpath
There is an nested iframe in that page, so you have to switch to iframe and again to iframe, and then you can send the keys to email address input field.
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(50)
driver.get("https://www.kingdoms.com/#logout")
wait = WebDriverWait(driver, 20)
try:
wait.until(EC.element_to_be_clickable((By.ID, "cmpbntyestxt"))).click()
except:
pass
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe.mellon-iframe")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src*='https://mellon-t5.traviangames.com/account/logout']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys('Mario#gmail.com')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))).send_keys("marios password")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='submit']"))).click()
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 desired fields are 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://www.kingdoms.com/#logout')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.cmpboxbtn.cmpboxbtnyes]"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.mellon-iframe")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://mellon-t5.traviangames.com/account/logout/applicationDomain']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys("Mario#Hooi.com")
driver.find_element_by_css_selector("input[name='password']").send_keys("MarioHooi")
Using XPATH:
driver.get('https://www.kingdoms.com/#logout')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='cmpboxbtn cmpboxbtnyes']"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='mellon-iframe']")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'https://mellon-t5.traviangames.com/account/logout/applicationDomain')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='email']"))).send_keys("Mario#Hooi.com")
driver.find_element_by_xpath("//input[#name='password']").send_keys("MarioHooi")
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
How To sign in to Applemusic With Python Using Chrome Driver With Selenium

How to click on the Create Account button within https://mail.protonmail.com/ signup page using Selenium and Python

Everything is working except the button. The submit button is not being clicked. Can anyone help me out? I believe it may have to do with the fact that I changed the frame to "top". But, I am unsure how to change it back.
Code:
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
import time
url = 'https://protonmail.com/'
driver = webdriver.Chrome('/Users/edenhikri/Desktop/chromedriver')
driver.get(url)
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn.btn-default.btn-short'))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.panel-heading'))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#freePlan'))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,'password'))).send_keys('test123')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,'passwordc'))).send_keys('test123')
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,".top")))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,'username'))).send_keys('myUsername')
driver.find_element_by_class_name('btn.btn-submit').click()
error:
no such element: Unable to locate element: {"method":"class name","selector":"btn.btn-submit"}
button code:
<button type="submit" class="btn btn-submit" name="submitBtn">Create Account</button>
I have also tried using xpath but I get the same error that it cannot find this:
driver.find_element_by_xpath("//button[#class=“btn btn-submit”]").click()
Please use this xpath //*[contains(text(),"Create Account")]
The CREATE ACCOUNT button 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 css-selectors based Locator Strategies:
driver.get("https://protonmail.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-default.btn-short[href='signup']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[data-plan='free']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#freePlan"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.top[title='Registration form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#username"))).send_keys("Eden")
driver.switch_to.default_content()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#password"))).send_keys("Eden")
driver.find_element_by_css_selector("input#passwordc").send_keys("Eden")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.bottom[title='Registration form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-submit"))).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:

Categories