A little trick makes it easy for Selenium to deal with scrollbars and element focus

Posted by Retired Bill on Wed, 08 Dec 2021 05:07:42 +0100

01 JS processing scroll bar

Execute via webdriver_ The script () method executes the JS script operation scroll bar

Right scroll bar

  • Firefox browser and Chrome browser use different syntax. It is listed in the following area. It looks like Google browser 71 general Firefox
# Firefox browser scrolls to the bottom
js = "var q=document.documentElement.scrollTop=10000"

# Firefox browser scrolls to the top
js = "var q=document.documentElement.scrollTop=0"
  
# Chrome browser scrolls to the bottom
js = "var q=document.body.scrollTop=10000"

# Chrome browser scrolls to the top
js = "var q=document.body.scrollTop=0"

# Cut in the scroll bar of the embedded form through the id attribute
js = "var q=document.getElementById('id').scrollTop=0"

Horizontal scroll bar:

#  x is the transverse distance and y is the longitudinal distance
js = "window.scrollTo(x,y);"

It is said that there is no compatibility problem with the scrollTo function

  • – scrollHeight gets the scroll height of the object.
  • – scrollLeft sets or gets the distance between the left boundary of the object and the leftmost end of the currently visible content in the window.
  • – scrollTop sets or gets the distance between the top of the object and the top of the visible content in the window.
  • – scrollWidth gets the scrollWidth of the object
# Scroll to bottom
js = "window.scrollTo(0,document.body.scrollHeight)"

# Scroll to top
js = "window.scrollTo(0,0)"

# Scroll to a position where the lateral distance is X and the longitudinal distance is Y
js = "window.scrollTo(x,y)"

Execute js script

driver.execute_script(js)

Last code:

# coding=utf-8
# author:Ven
     
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.set_window_size(width=800,height=700)
driver.get("https://www.baidu.com")
    driver.find_element_by_xpath("//input[@id='kw']").send_keys('selenium')
    driver.find_element_by_xpath("//input[@id='su']").click()
    sleep(1)
    js = "window.scrollTo(0,document.body.scrollHeight)"
    driver.execute_script(js)
    sleep(1)
    js = "window.scrollTo(500,0)"
    driver.execute_script(js)
    sleep(1)
    driver.quit()

02 element focus

Let the page jump directly to the position where the element appears through JS (pay attention to the covering phenomenon, focus on the covered elements and focus on the nearby elements)

target = driver.find_element_by_xpath()
driver.execute_script("arguments[0].scrollIntoView();", target)

# coding=utf-8
# author:Ven
     
from selenium import webdriver
from time import sleep
     
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
    driver.find_element_by_xpath("//input[@id='kw']").send_keys('selenium')
    driver.find_element_by_xpath("//input[@id='su']").click()
driver.set_window_size(width=600, height=600)
sleep(2)
target = driver.find_element_by_xpath("//*[@id='page']/a[1]/span[2]")
driver.execute_script("arguments[0].scrollIntoView();", target)             # Element focus to flip button - 2
sleep(2)
driver.quit()     

Official account: official account: you can receive a 216 page Software Test Engineer Interview treasure book document free of charge on the public address: sad hot strip. And free video sharing course, which includes basic knowledge, Linux, Shell, Internet program principle, Mysql database, packet capture tool, interface tester. Advanced Python programming, Web automation testing, APP automation testing, interface automation testing, advanced continuous integration of testing, test architecture development test framework, performance testing, security testing, etc.

Don't fight alone in learning. It's best to stay together and grow together. The effect of mass effect is very powerful. If we study together and punch in together, we will have more motivation to learn and stick to it.

Friends who like software testing, if my blog is helpful to you and if you like my blog content, please click "like", "comment" and "collect" for three times!

Haowen recommendation

Job transfer interview, job hopping interview, these interview skills that software testers must know!

Interview experience: move bricks in first tier cities! Face software testing post again, 5000 will be satisfied

Interviewer: I've worked for three years and have a preliminary test? I'm afraid your title of software test engineer needs double quotation marks

What kind of person is suitable for software testing?

The man who left work on time was promoted before me

The test post changed jobs repeatedly and disappeared

Topics: Firefox Selenium Programmer software testing chrome