I need a way to automatically scroll a browser window that i've opened using selenium code. I am currently using pyautogui but this requires the window to be selected. This is running on a virtual machine that i'm remoting into and when i toggle off the VM to work on my main desktop the code does not scroll so i need a way to auto scroll on a non active window.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import numpy as np
from pynput.mouse import Button, Controller
import win32api
import win32con
import pyautogui
import pandas as pd
import re
from selenium.common.exceptions import TimeoutException
pyautogui.FAILSAFE = False
s = Service(r'\\homedirpva1a01\USERSNC$\603225\Edgedriver\msedgedriver.exe')
browser = webdriver.Edge(service=s)
Options.page_load_strategy = 'eager'
time.sleep(5)
linklist=[]
browser.get('https://turo.com/us/en/search?country=US&defaultZoomLevel=11&delivery=false&deliveryLocationType=googlePlace&endDate=01/25/2023&endTime=10:00&isMapSearch=false&itemsPerPage=200&latitude=40.735657&location=Newark, New Jersey, USA&locationType=CITY&longitude=-74.1723667&pickupType=ALL&placeId=ChIJHQ6aMnBTwokRc-T-3CrcvOE®ion=NJ&sortType=RELEVANCE&startDate=01/22/2023&startTime=10:00&useDefaultMaximumDistance=true')
time.sleep(3)
print('start4434')
i4434=1
elems4434=[]
while i4434<50:
if 'An error occurred while searching, please try again' in browser.page_source:
listsave=['i4434','i4434']
save = pd.DataFrame(listsave)
save.to_csv(r'\\PATH\savetest.csv')
exit()
elif 'vehicle-card-link-box' not in browser.page_source:
listsave=['i4434','i4434']
save = pd.DataFrame(listsave)
save.to_csv(r'\\PATH\savetest.csv')
break
else:
elems4434=browser.find_elements(By.XPATH, '//a[@href]')
distinctlist = list(set(linklist))
frame = pd.DataFrame(distinctlist)
frame.to_csv(r'\\PATH\turolinklisttest.csv')
listsave=['i4434','i4434']
save = pd.DataFrame(listsave)
save.to_csv(r'\\PATH\savetest.csv')
for elem in elems4434:
if 'car-rental/united-states' in elem.get_attribute('href'):
linklist.append(elem.get_attribute('href'))
if 'suv-rental/united-states' in elem.get_attribute('href'):
linklist.append(elem.get_attribute('href'))
pyautogui.scroll(-1000)
time.sleep(3)
i4434 =1`
CodePudding user response:
You can use the Selenium execute_script
function to scroll the browser window, which does not require the window to be active. This function allows you to execute JavaScript code in the browser.
Here's an example of how you can use the execute_script
function to scroll the browser window in Selenium:
# scroll down by 1000 pixels
browser.execute_script("window.scrollBy(0, 1000);")
# scroll up by 1000 pixels
browser.execute_script("window.scrollBy(0, -1000);")
You can replace 1000 with the number of pixels you want to scroll.
You can also scroll to a specific element on the page using the scrollIntoView method.
# find the element you want to scroll to
element = browser.find_element_by_id("elementId")
# scroll to the element
browser.execute_script("arguments[0].scrollIntoView();", element)
You can also use this method to scroll to the bottom of the page by selecting the last element.
# find the last element on the page
element = browser.find_elements_by_xpath("//*")[-1]
# scroll to the last element
browser.execute_script("arguments[0].scrollIntoView();", element)
You can remove pyautogui.scroll(-1000)
and use the above execute_script
method to scroll down the page.