clicking on button in iframe with python - python

I am trying to access website with python selenium. I am going to place that I cant click on the button. any help please?
please find my code on the below:
from selenium import webdriver
from time import sleep
path_to_chromedriver = "chromedriver.exe"
driver = webdriver.Chrome(path_to_chromedriver)
url = 'https://www.mail.com/'
driver.get(url)
how to click on button "Agree and Continue" using css selector ?

The element Agree and Continue is present inside nested iframe you need to switch both the iframe first and then click on the element.
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it()
Induce WebDriverWait() and wait for element_to_be_clickable()
driver.get("https://www.mail.com/")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.permission-core-iframe")))
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://plus.mail']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='onetrust-accept-btn-handler']"))).click()
you need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Jump out from iframe you need to use.
driver.switch_to.default_content()

Related

How can I crawl Dynamic website with Selenium? There's error 'NoSuchElementException'

I've newly started to study Python and encountered NoSuchElementException while I'm doing crawling with SELENIUM.
I think target site is 'Dynamic webpage' hence I searched various way and tried but..... failed.
Would you help me? Thank in advance
Target site : http://www.kofiabond.or.kr/index_en.html
What I need : Click area on below picture and move to that page
enter image description here
Below is the code which I made:
from selenium import webdriver
driver=webdriver.Chrome('C:/Users/SMH/chromedriver.exe')
url='http://www.kofiabond.or.kr/index_en.html'
driver.get(url)
driver.maximize_window()
xpath = "//*[#id='genLv1_0_imgLv1']"
open_btn=driver.find_element_by_xpath(xpath)
driver.execute_script(open_btn)
To click on Final Quotation Yields link which is present inside an iframe you need to switch iframe first then hover on the main menu to element become visible on the page and then click on the element.
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it()
Induce WebDriverWait() and wait for visibility_of_element_located() and hover on the element
Induce WebDriverWait() and wait for element_to_be_clickable()
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
from selenium.webdriver import ActionChains
driver=webdriver.Chrome("C:/Users/SMH/chromedriver.exe")
driver.get('http://www.kofiabond.or.kr/index_en.html')
#Switch to iframe
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.NAME, 'fraAMAKMain')))
element=WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//a[./img[#alt='bond Interest Rates']]")))
#Hover main menu
ActionChains(driver).move_to_element(element).perform()
#Click on the element once become clickable
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[./div[text()='Final Quotation Yields']]"))).click()
Browser snapshot

Python Selenium pop-up

So, I've decided to make TikTok massfollower, but can't go further than login.
from selenium.webdriver import Firefox
from time import sleep
browser = Firefox()
browser.get('https://www.tiktok.com/foryou?lang=ru')
login = browser.find_element_by_class_name('jsx-3665539393')
login.click()
sleep(10)
login2 = browser.find_element_by_class_name('channel-name-2Dwny')
login2.click()
It goes to TikTok website, then it presses the log-in button, and then there is pop-up window(I hope that's the right way to call it). But program can't find elements on this pop-up window, I thought that I should wait for pop-up to load, but there is no difference.
The element you are after is inside an iframe you need to switch to iframe first in order to access the element.
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it() and following css selector.
Induce WebDriverWait() and wait for element_to_be_clickable()
browser.get('https://www.tiktok.com/foryou?lang=ru')
#Click on Login
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a.jsx-3665539393"))).click()
#Click cookeis button
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.button-wrapper>button"))).click()
WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='tiktok']")))
browser.execute_script("arguments[0].click();",WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.close-modal-8dfIo"))))
You need to import following libraries
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

selecting elements through Selenium is not working

There are HTML elements on the website (https://www.immowelt.de/expose/2ult44w) that Selenium does not recognize at all. But I would like to be able to address them. I still recognize the element "body" without any problems, however "div [# class = 'cdk-overlay-container']" is not. Errors are not thrown.
from selenium import webdriver
import time
driver = webdriver.Chrome('C:\\go2\\installation\\chromedriver.exe')
driver.get("https://www.immowelt.de/expose/2ult44w");
driver.execute_script("return document.readyState") == "complete"
time.sleep(10)
#just so that a message is clicked away:
datenschutz = driver.find_elements_by_xpath("//button[#id='uc-btn-accept-banner']")
if len(datenschutz) > 0: datenschutz[0].click()
#that is not recognized:
example = driver.find_elements_by_xpath("//div[#class='cdk-overlay-container']")
print("Counts:"+str(len(example))) #result: Counts:0
The reason you are unable to found it because the element is present inside an iframe.
You need to switch to iframe first.
To Switch to iframe
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it()
And
Induce WebDriverWait() and wait for presence_of_all_elements_located()
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome('C:\\go2\\installation\\chromedriver.exe')
driver.get("https://www.immowelt.de/expose/2ult44w")
btn=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[#id='uc-btn-accept-banner']")))
driver.execute_script("arguments[0].click();", btn)
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"externalViewerStage")))
example=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.XPATH,"//div[#class='cdk-overlay-container']")))
print("Counts:"+str(len(example)))
driver.switch_to.default_content()

python selenium, cant't click element

i'm having a problem with selenium. I load this page: Investing 3M, and i want to click got it in a pop-out window.
I already tryed with this code bellow, but it doesn't work:
driver = webdriver.Chrome()
driver.get("https://www.investing.com/equities/3m-co-financial-summary")
driver.implicitly_wait(10)
x =driver.find_elements_by_xpath("/html/body/div[8]/div[1]/div/div[2]/div[2]/a[1]")
print(len(x))
print(x)
Can anyone provide any solution to this?
The element is present inside an iframe.You need to switch to frame first.
To handle dynamic element you need to
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it() and following xpath
Induce WebDriverWait() and wait for element_to_be_clickable() and following xpath
code:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.investing.com/equities/3m-co-financial-summary")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='TrustArc Cookie Consent Manager']")))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Got it']"))).click()
Browser snapshot after clicking

Selenium (Python) find_element_by_id does not work on schwab.com

Using Selenium (via Python), I am trying to locate the "Login" button of http://schwab.com.
The button is an element of type BUTTON and id='loginSubmitButton'.
I am using the following code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://schwab.com")
driver.implicitly_wait(10)
driver.find_element_by_id("loginSubmitButton")
driver.close()
The browser correctly opens the page and the button is verifiably there (using Chrome dev tools), however Selenium fails to locate it.
I have tried many variations of this code, including using WebDriverWait, but nothing seems to work.
Suggestions are quite appreciated.
You are unable to click on that button because login form is under iFrame.
So first you need to switch into iframe, then only you'll be able to access those elements
username_frame = driver.find_element_by_id('LoginComponentForm')
driver.switch_to.frame(username_frame)
driver.find_element_by_id("loginSubmitButton").click()
As the the desired element is within an <iframe> so to invoke click() on the element 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:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('https://www.schwab.com/')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#LoginComponentForm")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#LoginText"))).click()
Using XPATH:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('https://www.schwab.com/')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='LoginComponentForm']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[#id='LoginText']"))).click()
Reference
Here you can find a relevant discussion on Ways to deal with #document under iframe
Using find_element_by_xpath is more efficient than using find_element_by_id.
You can try using this
Also the login form is displayed on the iframe.
You need to switch to the iframe to click the button.
and then switch back to the default to be able to click or do actions on other elements outside the frame.
For switching to iframe you can use the following code :
driver.switch_to.frame(iframe)
Then use your click button code here.
For switching back to default content, use this line :
driver.switch_to.default_content()
This should work.

Categories

Resources