Selenium is not omnipotent, and sometimes the operation of the page can not be achieved, this time you need to use JS to complete.
Common scenarios:
When the elements on the page more than one screen, want to manipulate the elements below the screen, is not directly located, will be reported that the element is not visible.
You need to use the scroll bar to drag the screen so that the manipulated element is displayed on the current screen.
Scroll bars cannot be positioned directly with the positioning tool. Selenium there is no direct way to control the scroll bar,
This time only with the help of J, fortunately selenium provides a way to operate JS:
Execute_script (), you can directly execute the JS script.
First, JavaScript introduction
1.JavaScript is the most popular scripting language in the world because all of the pages you browse on your computer, phone, tablet,
and countless HTML5-based mobile apps, the interactive logic is driven by JavaScript. To put it simply,
JavaScript is an interpreted type of programming language that runs in a browser.
So the question is, why should we learn JavaScript?
2. Some special operation Selenium2+python can not be directly completed, JS is just the strength of this area, so it is a very
Good to Add. JS is not familiar with, you can find the next tutorial online, simple to understand some.
Http://www.w3school.com.cn/js/index.asp4
Second, control the height of the scroll bar
1. Scroll bar back to top:
Js= "var Q=document.getelementbyid (' id '). scrolltop=0"
Driver.execute_script (JS)
2. Scroll bar to the bottom
Js= "var q=document.documentelement.scrolltop=10000"
Driver.execute_script (JS)
3. Here you can modify the value of the scrolltop, to locate the right scroll bar position, 0 is the top, 10000 is the bottom.
All rights reserved, public number: Software Testing tribe
Three, horizontal scroll bar
1. Sometimes the browser page needs to scroll left and right (after the general screen is maximized, the left and right scrolling conditions are rarely seen).
2. Control the horizontal and vertical scrollbar by the left ScrollTo (x, y) js = "Window.scrollto (100,400);"
Driver.execute_script (JS)
3. The first parameter x is the transverse distance, the second parameter y is the longitudinal distance
Four, Chrome browser
1. The above method is available on Firefox, but it is not used with Chrome browser.
Google Browser is so wayward, not obedient, so use the following methods to solve the Google browser scroll bar problem.
2.Chrome Browser Workaround:
JS = "var q=document.body.scrolltop=0"
Driver.execute_script (JS)
Five, Element focus
1. Although the above method can solve the position problem of dragging scroll bar, but sometimes it is impossible to determine the elements I need to manipulate
In what position, it is possible to open a different page each time, the location of the element is not the same, how to do?
2. At this point we can let the page jump directly to the location where the element appears, and then we can manipulate it. Also need to use JS to achieve.
3. Elemental Focus:
target = Driver.find_element_by_xxxx ()
Driver.execute_script ("Arguments[0].scrollintoview ();", target)
VI. Get Browser name: Driver.name
1. In order to solve the different browser operation method is not the same problem, you can write a function to do compatibility.
2. First use Driver.name to get the browser name, and then use the IF statement to make a judgment
VII. Compatibility
1. Compatible with Google and firefox/ie
Eight, the reference code is as follows:
# Coding:utf-8
From selenium import Webdriver
Driver = Webdriver. Firefox ()
Driver.get ("https://www.baidu.com")
Print Driver.name
# Back to the top
Def scroll_top ():
if driver.name = = "Chrome":
JS = "var q=document.body.scrolltop=0"
Else
JS = "var q=document.documentelement.scrolltop=0"
Return Driver.execute_script (JS)
# Pull to the bottom
Def scroll_foot ():
if driver.name = = "Chrome":
JS = "var q=document.body.scrolltop=10000"
Else
JS = "var q=document.documentelement.scrolltop=10000"
Return Driver.execute_script (JS)
# focus Element
target = Driver.find_element_by_xxxx ()
Driver.execute_script ("Arguments[0].scrollintoview ();", target)
PYTHON+SELENIUM+JS Handling scroll bars