Frame Switch to click on a Box (Python Selenium) - python

I have a problem trying to automate my browser with Selenium on Python. It's been several hours that I block, and since I'm a beginner .. :(
I explain my problem:
I have to reach click on a box of Recaptcha. To do this, my bot must click on a button on the site, which then displays the recaptcha that I have to validate.
Here are the source page screenshot:
The popup of the recaptcha, in which the checkbox is located
The location of the checkbox that I have to click
I try this code:
time.sleep(5)
browser.switch_to_frame(browser.find_element_by_tag_name("CaptchaPopup"))
browser.switch_to_frame(browser.find_element_by_tag_name("iframe"))
CheckBox = WebDriverWait(browser, 10).until(
browser.find_element_by_id('recaptcha-anchor').click())
time.sleep(0.7)
CheckBox.click()
But the latter returns me an error :(
selenium.common.exceptions.NoSuchFrameException: Message: no such frame
I use Python 2.7.
Do you have a solution ?
Thank you very much in advance!

Try to use below code to handle required check-box:
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
wait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it(browser.find_element_by_xpath('//iframe[contains(#src, "google.com/recaptcha")]')))
wait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'recaptcha-anchor'))).click()

Related

element click intercepted (click work perfectly)(selenium python)(popup)

I'm trying to automate a website using python selenium.
# ----- click on the sign up popup
popup = driver.find_element_by_xpath("/html/body/div[1]/div[4]/section[1]/div/div/div[2]/div/p/a")
popup.click()
click is working fine and the pop-up message is showing perfectly. but after that my I get this error:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (1263, 589). Other element would receive the click: <div class="mailmunch-inner-overlay" id="mailmunch-inner-overlay-178218df563cebd"></div>
(Session info: chrome=89.0.4389.82)
and the code stop working. what can I do?
please help!
You need to get rid of the overlay. Without the HTML I can only offer a couple of solutions:
Find the dismiss button of the overlay, click it.
Use Javascript to remove the overlay from the DOM
Wait until the overlay disappears
Could it be that you are receiving a described error after you are trying to find an element from that pop-up window? So it's not the line you are thinking that is producing the error. If that is the case, then when you are trying to for example click something in that pop-up window, try the following:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,""))).click()
You need to import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
popup = driver.find_element_by_xpath("/html/body/div[1]/div[4]/section[1]/div/div/div[2]/div/p/a")
driver.execute_script("arguments[0].click();", popup)
Try invoking click directly to the element so that it bypasses the overlay or click it's close button.

How to click the Continue button within a website using selenium in python?

I am trying to write a code that is able to auto apply on job openings on indeed.com. I have managed to reach the last stage, however, the final 2 clicks on the application form is giving me a lot of trouble. Please refer to the first page as below
Once we click on continue on the first page, for the second page I first need to scroll down a bit to reach from here...
..to here and then finally click on apply.
I am stuck on the first page, as the click function does not do anything. I have written the following code:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
driver.find_element_by_xpath('//*[#id="apply-button-container"]/div[1]/span[1]').click()
time.sleep(5)
frame_1 = driver.find_element_by_css_selector('iframe[title="Job application form container"')
driver.switch_to.frame(frame_1)
frame_2 = driver.find_element_by_css_selector('iframe[title="Job application form"]')
driver.switch_to.frame(frame_2)
continue_btn = driver.find_element_by_css_selector('#form-action-continue')
continue_btn.click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='form-action-continue']"))).click()
driver.find_element_by_xpath('//button[#id="form-action-continue"]').click()
I have tried switching the iframes again for this step but nothing happens. Even the .click() function does not do anything.
Will appreciate some help on this.
This should go through the first click if your values are inputted.
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.indeed-apply-button"))).click()
frame_1 = driver.find_element_by_css_selector('iframe[#title="Job application form container"')
driver.switch_to.frame(frame_1)
frame_2 = driver.find_element_by_css_selector('iframe[#title="Job application form"]')
driver.switch_to.frame(frame_2)
cont="#form-action-continue"
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, cont))).click()

How to select an option from dropdown

I have a problem navigating a website using selenium. This is my code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://webtrader.binary.com/v2.2.8/main.html#')
resources = driver.find_element_by_id('ui-id-1')
resources.click()
However, I get the exception:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <ul id="ui-id-1" class="ui-menu ui-widget ui-widget-content ui-menu-icons"> could not be scrolled into view
I don't understand where I went wrong. I am trying to access 'Historical data' from the dropdown menu labeled "Resources". Could someone please help me access it. Maybe I got the id for Resources wrong. You could also check that out.
The element you want to click to open the dropdown is the previous sibling of the element resources
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import By
from selenium.webdriver.support.ui import WebDriverWait
wait = WebDriverWait(driver, 10)
driver.find_element_by_class_name('resources').click()
historical_data = wait.until(ec.visibility_of_element_located((By.ID, 'ui-id-4')))
There are many things happening here. First of all, your code is lacking a wait. Without it will always fail, as the page is dynamically loaded. Read about Waits.
Secondly, here resources = driver.find_element_by_id('ui-id-1') you are finding the element from the dropdown menu, and then you are trying to click it. But the dropdown menu is not opened. You should click on it, then wait for the option to appear, only then click on the 'Historical data'.

Selenium click hidden button

I want to control webpage with selenium and python
Python code;
menu = browser.find_element_by_css_selector(".nav")
hidden = browser.find_element_by_link_text('Soluton')
element = browser.find_element_by_xpath("//div[#class=\"two-columns\"]")
Option 1: ActionChains(browser).move_to_element(menu).click(hidden)
Option 2 : ActionChains(browser).move_to_element(element).click(hidden)
html code;
I want to click "Solution" button under the nav menu.
However, Solution be in under the nav menu. So it's hidden.
so, I type following codes ;
 Option 1:
ActionChains(browser).move_to_element(menu).click(hidden)
 Option 2 :
ActionChains(browser).move_to_element(element).click(hidden)
But selenium not happen anything and not give any error message.
How can I click a button under nav menu with selenium ?
Thanks
If I have understood your Question and your Requirement we need to Mouse Hover over the WebElement with text as Actions where 2 options pops out as Request and Solution and you want to click() on Solution. To achieve that you can use the following code block :
#import
from selenium.webdriver.common.action_chains import ActionChains
#code block
menu = driver.find_element_by_xpath("//a[#class='incident-action']/i[#class='icon-ellipsis-h']")
ActionChains(driver).move_to_element(menu).perform()
driver.find_element_by_xpath("//div[#class='action-list' and #id='header-actions-list']//a[#class='icon-arrow' and contains(text(),'Solution')]").click()
You can try to click Actions to make actions list visible and then click on required Solution option:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
actions = wait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Actions")))
actions.click()
solution = wait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Solution")))
solution.click()

Selenium with Python - Switching to main content after popup overlay

After handling a pop up overlay the rest of the code no longer works.
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.vapordna.com/login.asp')
#handling popup
browser.find_element_by_css_selector("div.age-verify-button.age-modal__btn.btn.btn-primary").click()
browser.find_element_by_link_text("Yes").click()
If I don't close the pop up the rest of the code works, if I close the popup none of the code after that point works.
emailElem = browser.find_element_by_name("email")
emailElem.send_keys("****#****.com")
passwordElem = browser.find_element_by_name("password")
passwordElem.send_keys("******")
passwordElem.submit()
I have tried adding this line but it doesn't help,
browser.switchTo().defaultContent();
To my understanding the popup isn't in a new iframe or window so I'm not sure why there would be any problem with continuing to interact with the page.
I also tried clicking on the body before continuing
browser.find_element_by_tag_name("body").click()
After seeing your website I observe this is neither an frame nor a window, I just a form so there is no need to use here browser.switchTo().defaultContent(); which actually using for switching from any frame to default, Actually when you are going to find this button on the opened popup which overlay all the page, It would not be full loaded on the page, so you should try using WebDriverWait to wait until this button visible on the page.
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
browser = webdriver.Firefox()
browser.get('https://www.vapordna.com/login.asp')
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.age-verify-button.age-modal__btn.btn.btn-primary"))).click()
Now after clicking on this agreement button you can proceed further steps for login.

Categories