How to insert data in frame using selenium - python

I am using selenium with python I want to insert the username and email inputs containing in frame
Here is the link
I am trying to do this but it's not switching into the modal.
try:
time.sleep(3)
driver.switch_to.frame(driver.find_element_by_id("thanksModal"))
driver.find_element_by_xpath("//input[#type='file']").send_keys("C:\\Users\\user\\Desktop\\Resume.docx")
driver.find_element_by_id('txtName').send_keys(name)
driver.find_element_by_id('txtEmail').send_keys(email)
driver.find_element_by_id('btnSubmit').click()
time.sleep(5)
except:
pass

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://www.collabera.com/find-a-job/search-jobs/job-details/248429-python-developer-jobs-jersey-city-nj/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.apply-form")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#txtName"))).send_keys("bhupathi_turaga#stackoverflow.com")
driver.find_element_by_css_selector("input#txtEmail").send_keys("bhupathi_turaga#stackoverflow.com")
Using XPATH:
driver.get("https://www.collabera.com/find-a-job/search-jobs/job-details/248429-python-developer-jobs-jersey-city-nj/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='apply-form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='txtName']"))).send_keys("bhupathi_turaga#stackoverflow.com")
driver.find_element_by_xpath("//input[#id='txtEmail']").send_keys("bhupathi_turaga#stackoverflow.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:
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

Related

Python Selenium: Unable to find xpath to click on element within iframe

I'm using this code but the can't get it to work with the xpath:
browser = webdriver.Chrome(chrome_path)
browser.get("https://planetradio.co.uk/cool-fm")
time.sleep(5)
browser.find_element_by_xpath('//*[#id="notice"]/div[4]/button[2]').click()
The element ACCEPT ALL 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://planetradio.co.uk/cool-fm/')
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='ACCEPT ALL']"))).click()
Using XPATH:
driver.get('https://planetradio.co.uk/cool-fm/')
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='ACCEPT ALL']"))).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:
References
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

How to click an element that contains visible text and is clickable with Selenium with Python

The element to be clicked based ONLY on the text it contains (others):
<a class="blah" href="/some_page/"><span>15</span> others</a>
The item that fails:
driver.find_element(By.XPATH, "//*[contains(text(), ' others')]").click()
The error:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(text(), 'others')]"}
An additional alternative from DebanjanB's answer using a different xpath inspired from XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode
"//*[text()[contains(.,' others')]]"
To locate 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 CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.blah[href='/some_page/'] > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='blah' and #href='/some_page/'][contains(., 'others')]"))).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
References
You can find a couple of relevant discussions on NoSuchElementException in:
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

Getting id or xpath of parts of a website

I want to use selenium to login in the website https://www.winamax.es/account/login.php?redir=/apuestas-deportivas. The case is that I don't find the xpath/id/text to get te next code running successfully:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
options = webdriver.ChromeOptions()
options.add_argument("window-size=1920,1080")
options.add_argument('--disable-blink-features=AutomationControlled')
driver=webdriver.Chrome(options=options,executable_path=r"chromedriver.exe")
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver=driver, timeout=15).until(
lambda x: x.execute_script("return document.readyState === 'complete'")
)
upload_field = driver.find_element_by_xpath("//input[#type='email']")
I don't only want the specific xpath for this example, but I prefer a method to obtain the xpath or something similar to get the code working for other parts of the website
<iframe id="iframe-login" data-node="iframe" name="login" scrolling="auto" frameborder="1" style="min-height: 280px; width: 100%;"></iframe>
Your element is in an iframe. Switch to it.
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe-login")))
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Full working code.
wait = WebDriverWait(driver, 10)
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe-login")))
upload_field = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#type='email']")))
upload_field.send_keys("stuff")
The email 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:
Using CSS_SELECTOR:
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#iframe-login")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='email']"))).send_keys("scraper#stackoverflow.com")
Using XPATH:
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='iframe-login']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#type='email']"))).send_keys("scraper#stackoverflow.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:
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

Not able to click a link button using selenium gives error element not found exception

I am trying to click on the ARCGIS button on webpage https://gisapr.atco.com/portal/home/signin.html?returnUrl=https%3A%2F%2Fgisapr.atco.com%2Fportal%2Fhome%2F
But getting no element found or timeout errors.
I'm also unable to access inside login form contents but no luck.
Tried:
Implicit and explicit waits
Time sleep()
by all finds..
The element ARCGIS 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://gisapr.atco.com/portal/home/signin.html?returnUrl=https%3A%2F%2Fgisapr.atco.com%2Fportal%2Fhome%2F')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#oAuthFrame")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn_wide#ago_Name"))).click()
Using XPATH:
driver.get('https://gisapr.atco.com/portal/home/signin.html?returnUrl=https%3A%2F%2Fgisapr.atco.com%2Fportal%2Fhome%2F')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='oAuthFrame']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#id='ago_Name' and text()='ArcGIS']"))).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 Error in Python - Message: no such element: Unable to locate element

I am trying to get data from a password protected website with Selenium.
However I get stuck right in the beginning at login with the following error message:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="username"]"}
(Session info: chrome=87.0.4280.66)
The name I use is correct for sure, I inspected the website. Including waiting time did not help either...
My code is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from getpass import getpass
driver = webdriver.Chrome()
driver.get("https://clearing.apcs.at/emwebapcsem/startApp.do")
print(driver.title)
print(driver.current_url)
# create an object for searchbox
username=driver.find_element_by_name("username")
password=driver.find_element_by_name("password")
# typte the input
username.send_keys("XXXXXX")
password.send_keys("XXXXXX")
driver.find_element_by_name('login').click()
Any suggestions would be appreciated.
To send a character sequence to the Benutzer and Passwort field as the elements are within an <frame> 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://clearing.apcs.at/emwebapcsem/startApp.do')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[title='menu']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.loginContentBoxInput[name='username']"))).send_keys("Endre")
driver.find_element_by_css_selector("input.loginContentBoxInput[name='password']").send_keys("Endre")
Using XPATH:
driver.get('https://clearing.apcs.at/emwebapcsem/startApp.do')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[#title='menu']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='loginContentBoxInput' and #name='username']"))).send_keys("Endre")
driver.find_element_by_xpath("//input[#class='loginContentBoxInput' and #name='password']").send_keys("Endre")
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

Categories

Resources