Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to make a split screen scroll to the bottom

I've written a script in python in combination with selenium to make the screen of a webpage scroll downward. The content is within the left-sided window. If I scroll down, more items are visible. I've tried with the below approach but It doesn't seem to work. Any help on this will be highly appreciated.

Check out this: website link.

What I've tried so far with:

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


driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("find_the_link_above")

elem = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#pannello-espositori .luogo")))

for item in range(3):
    elem.send_keys(Keys.END)
    time.sleep(3)

driver.quit()

When I execute the above script, it throws an exception can't focus element.

like image 319
SIM Avatar asked Apr 06 '18 19:04

SIM


1 Answers

Try this. You can see scrolling effect by scrolling up to the elements in the left panel.

This solution would scroll up to first 100 elements.

from selenium import webdriver
import time

def scroll_element_into_view(element):
    driver.execute_script(
        "arguments[0].scrollIntoView(true);",
        element)
    time.sleep(0.2) #increase/decrease time as you want delay in your view

driver = webdriver.Chrome()
driver.maximize_window()
driver.set_page_load_timeout(5)
try:
    driver.get("http://catalogo.marmomac.it/it/cat")
    time.sleep(3)
    total_elems= driver.find_elements_by_css_selector(".scroller .elemento")
    print len(total_elems)
    for i in range(len(total_elems)):
        scroll_element_into_view(total_elems[i])
except Exception as e:
    print e
finally:
    driver.quit()

As you have mentioned, after scroll it would load more elements.Below script would handle that too. Here we can use total count which already shown at top of the panel.

for ex count is : 1669

  1. First it will scroll from 1 to 100 element
  2. Again find total elements which is now 150
  3. So it will scroll from 101 to 150
  4. Again find total elements which is now 200
  5. So it will scroll from 150 to 200

this process would continue till 1669 element. (Store previous count in one variable and update it after every loop)

try:
    driver.get("http://catalogo.marmomac.it/it/cat")
    time.sleep(3)
    total_elems=0
    total_count = int(driver.find_element_by_css_selector(".totali").text)
    while total_elems<total_count:
        elems= driver.find_elements_by_css_selector(".scroller .elemento")
        found_elms= len(elems)
        for i in range(total_elems,found_elms):
            scroll_element_into_view(elems[i])
        total_elems=found_elms
except Exception as e:
    print e
finally:
    driver.quit()
like image 109
Chanda Korat Avatar answered Sep 21 '22 12:09

Chanda Korat