Python + selenium + Js processing wheel Moving Bar

Source: Internet
Author: User

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.runoob.com/js/js-tutorial.html

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

?
1

八、参考代码如下:

#Coding:utf-8 fromSeleniumImportWebdriverdriver=Webdriver. Firefox () Driver.get ("https://www.baidu.com")PrintDriver.name#back to topdefscroll_top ():ifDriver.name = ="Chrome": JS="var q=document.body.scrolltop=0"Else: JS="var q=document.documentelement.scrolltop=0"returndriver.execute_script (JS)#pull to the bottomdefscroll_foot ():ifDriver.name = ="Chrome":#Chrome uses this approachJS ="var q=document.body.scrolltop=10000"Else:#Other browsers Use this methodJS ="var q=document.documentelement.scrolltop=10000"returndriver.execute_script (JS)#Focus Elementtarget =driver.find_element_by_xxxx () driver.execute_script ("Arguments[0].scrollintoview ();", target)

 

JS功能还是很强大的,它还可以处理富文本、内嵌滚动条的问题。

Python + selenium + Js processing wheel Moving Bar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.