martifapa
martifapa

Reputation: 117

Scroll down not working properly (Python)

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!

Upvotes: 2

Views: 4782

Answers (2)

KunduK
KunduK

Reputation: 33384

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

Upvotes: 2

SeleniumUser
SeleniumUser

Reputation: 4177

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']")))

Upvotes: 1

Related Questions