2

I'm trying to scroll down a webpage to avoid a 'MoveTargetOutOfBoundsException', but no matter what I try the webpage will only scroll down a couple inches.

Here the solutions I've tried, but haven't worked:

#1
element = driver.find_element_by_xpath('//*[@id="advisor"]')
element.location_once_scrolled_into_view

#2
from selenium.webdriver.common.keys import Keys
html = browser.find_element_by_tag_name('html')
html.send_keys(Keys.END)

#3 No matter what number I use for Y, the scroll down will always be the same
driver.execute_script("window.scrollTo(0, Y)")

The webpage I'm trying where I'm trying to scroll down is: https://www.bbva.es/personas/productos/prestamos/simulador-prestamos-personales.html?cid=sem:br:ggl:spain---consumo-prestamo_personal-open-:br_topperformance_open_ex-consideration:::bbva_prestamos:e:::text::&scp=true&gclid=Cj0KCQjw6sHzBRCbARIsAF8FMpWbbhXlRD8SluBVkA9-qRsuTSNQxn2E6avbmeDVRnb02gs1BnRef30aAtktEALw_wcB

Thanks in advance!

2 Answers 2

2

The element is present inside an iframe.You need to switch it first.

driver.get("https://www.bbva.es/personas/productos/prestamos/simulador-prestamos-personales.html?cid=sem:br:ggl:spain---consumo-prestamo_personal-open-:br_topperformance_open_ex-consideration:::bbva_prestamos:e:::text::&scp=true&gclid=Cj0KCQjw6sHzBRCbARIsAF8FMpWbbhXlRD8SluBVkA9-qRsuTSNQxn2E6avbmeDVRnb02gs1BnRef30aAtktEALw_wcB")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.iframe__base")))
element=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//div[@id="advisor"]')))
element.location_once_scrolled_into_view

You need to import following libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

UPDATE

Here is the complete code.To enable calculator button you need to enter value in amount text box.However normal webdriver click is not working hence used javascripts executor.

driver=webdriver.Chrome()
driver.get("https://www.bbva.es/personas/productos/prestamos/simulador-prestamos-personales.html?cid=sem:br:ggl:spain---consumo-prestamo_personal-open-:br_topperformance_open_ex-consideration:::bbva_prestamos:e:::text::&scp=true&gclid=Cj0KCQjw6sHzBRCbARIsAF8FMpWbbhXlRD8SluBVkA9-qRsuTSNQxn2E6avbmeDVRnb02gs1BnRef30aAtktEALw_wcB")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.iframe__base")))
element=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//div[@id="advisor"]')))
element.location_once_scrolled_into_view
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//input[@id="amount"]'))).send_keys("25000")
elemen1=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//label[@for='cli-no']/span[text()='No']")))
driver.execute_script("arguments[0].click();", elemen1)
button=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[@id='next']")))
driver.execute_script("arguments[0].click();", button)

snapshot:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Hi KunduK! In my script I already had those libraries imported. The problem I have is that, if I switch back from the iframe and scroll down, I'm not able to click on the button I want to because it is inside the iframe I switch back from. In turn, if after scrolling down I switch back to the iframe the page automatically scrolls up to the top of the page (and I cannot click the button because it's out of the viewport)!
@martifapa : Ok which you would to click can you mention?
Sure! "No" and "Calcular cuota"
Great!!!Please up vote the answer if this helps future reader.Thanks!!!
1

There are multiple ways to scroll down on web page. Currently ifrane is present on your web page you need to swwitch control to iframe before scrolling try below code section for your ref:

1. you can also use this to scroll down to the bottom of the page.

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

2. You can use also use ActionChains to move to elements

hover = ActionChains(driver)
hover.move_to_element(menu).perform()

3. Based on height

driver.execute_script("window.scrollTo(0, Y)")

where Y is the height (on a fullhd monitor it's 1080).

Working code :

driver.maximize_window()

wait = WebDriverWait(driver, 10)
driver.get("https://www.bbva.es/personas/productos/prestamos/simulador-prestamos-personales.html?cid=sem:br:ggl:spain---consumo-prestamo_personal-open-:br_topperformance_open_ex-consideration:::bbva_prestamos:e:::text::&scp=true&gclid=Cj0KCQjw6sHzBRCbARIsAF8FMpWbbhXlRD8SluBVkA9-qRsuTSNQxn2E6avbmeDVRnb02gs1BnRef30aAtktEALw_wcB")
driver.find_element_by_tag_name('body').send_keys("Keys.ESCAPE")
driver.switch_to.frame("content-iframe")
driver.execute_script("window.scrollTo(0, Y)")
inputBox = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='advisor']")))

4 Comments

I also tried that before,but didn't work... Any idea why?
I already tried all combinations I could find of driver.execute_script(), but all I get is a small scroll down... The error I get is "MoveTargetOutOfBoundsException: move target out of bounds" (since I'm trying to click a button which is outside the viewport.
you are in the iframe first switch to frame and then you can use scrolling script
You were right! Switching back to the default content lets me to scroll down, but then I can't click the button I want (because it is inside the iframe I switch back from). If, after scrolling, I switch to the iframe again, the page automatically scrolls up to the top and I end up with the same problem... What I want to do is click on the two buttons "No" and "Calcular cuota"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.