Skip to content

Commit ab94e86

Browse files
committed
initial commit
0 parents  commit ab94e86

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# How to scroll webpage in automation test in Python-selenium on [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=Python-selenium-scroll)
2+
3+
If you want to test scrolling of a webpage using an automation test in Python-selenium on LambdaTest, you can use the follwing steps. You can refer to sample test repo [here](https://github.com/LambdaTest/python-selenium-sample).
4+
5+
# Steps
6+
7+
## Scrolling to bottom of the page
8+
9+
To scroll to the bottom of the page, you can use the following line of code (refer to `scroll.py` for full code):
10+
11+
```python
12+
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
13+
```
14+
15+
## Infitinite scroll
16+
17+
If you want **to scroll to a page with infinite loading**, like social network ones, facebook etc. You can use the following code (refer to `lambdatest.py` for full code):
18+
19+
```python
20+
SCROLL_PAUSE_TIME = 0.5
21+
22+
# Get scroll height
23+
last_height = driver.execute_script("return document.body.scrollHeight")
24+
25+
#controls how many times scrolled to bottom
26+
scroll_pass = 0
27+
28+
#change to True for infinite scroll
29+
while scroll_pass < 10:
30+
# Scroll down to bottom
31+
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
32+
33+
# Wait to load page
34+
time.sleep(SCROLL_PAUSE_TIME)
35+
36+
# Calculate new scroll height and compare with last scroll height
37+
new_height = driver.execute_script("return document.body.scrollHeight")
38+
if new_height == last_height:
39+
break
40+
last_height = new_height
41+
scroll_passs+=1
42+
```
43+
## Run your test
44+
45+
```bash
46+
python lambdatest.py
47+
```
48+
49+
# Links:
50+
51+
[LambdaTest Community](http://community.lambdatest.com/)
52+

lambdatest.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import os
2+
import unittest
3+
import sys
4+
from selenium import webdriver
5+
6+
username = os.environ.get("LT_USERNAME")
7+
access_key = os.environ.get("LT_ACCESS_KEY")
8+
9+
10+
class FirstSampleTest(unittest.TestCase):
11+
12+
# setUp runs before each test case
13+
def setUp(self):
14+
desired_caps = {
15+
'LT:Options': {
16+
"user": username,
17+
"accessKey": access_key,
18+
"build": "Python-Selenium-Sample",
19+
"name": "Python-Selenium-Test",
20+
"platformName": "Windows 11",
21+
"selenium_version": "4.0.0"
22+
},
23+
"browserName": "Chrome",
24+
"browserVersion": "latest",
25+
}
26+
27+
self.driver = webdriver.Remote(
28+
command_executor="http://hub.lambdatest.com:80/wd/hub",
29+
desired_capabilities=desired_caps)
30+
31+
32+
# tearDown runs after each test case
33+
34+
def tearDown(self):
35+
self.driver.quit()
36+
37+
def scroll_bottom():
38+
"""
39+
Verify scrolling to bottom
40+
:return: None
41+
"""
42+
driver.get('https://www.lambdatest.com/')
43+
driver.maximize_window()
44+
sleep(2)
45+
success = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
46+
sleep(5)
47+
assert success == True
48+
49+
def scroll_infinite():
50+
"""
51+
Verify infinite scroll
52+
:return: None
53+
"""
54+
SCROLL_PAUSE_TIME = 0.5
55+
56+
# Get scroll height
57+
last_height = driver.execute_script("return document.body.scrollHeight")
58+
59+
#controls how many times scrolled to bottom
60+
scroll_pass = 0
61+
62+
#change to True for infinite scroll
63+
while scroll_pass < 10:
64+
# Scroll down to bottom
65+
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
66+
67+
# Wait to load page
68+
time.sleep(SCROLL_PAUSE_TIME)
69+
70+
# Calculate new scroll height and compare with last scroll height
71+
new_height = driver.execute_script("return document.body.scrollHeight")
72+
if new_height == last_height:
73+
break
74+
last_height = new_height
75+
scroll_pass+=1
76+
77+
78+
79+
if __name__ == "__main__":
80+
unittest.main()

0 commit comments

Comments
 (0)