Selenium-webdriver (Python) (ii) How to print URLs

Source: Internet
Author: User
Tags execution mail sleep

Print URL

As mentioned in the previous section, you can print out the title of the browser, and here's a simple one to print the current URL. In fact, there is no big use, you can do a dine use case.

#coding =utf-8 from
    
Selenium import webdriver
import time
    
browser = Webdriver. Firefox ()
    
url= ' http://www.baidu.com '
    
#通过get方法获取当前URL打印
print ' Now access%s '% (URL)
browser.get ( URL)
    
time.sleep (2)
browser.find_element_by_id ("kw"). Send_keys ("Selenium")
browser.find_element_by _id ("su"). Click ()
time.sleep (3)
browser.quit ()

In fact, we can put this user login successfully after the URL print, used to verify the user login successfully.

Alternatively, we print other information, for example, the General Login Success page will appear "Welcome + username", you can print this information to indicate that the user logged in successfully. (How to achieve, you think about it yourself ~! )

Maximize your browser

We know that the browser that invoked the startup is not full-screen, so it does not affect the execution of the script, but sometimes it affects the execution of the "watch" script.

#coding =utf-8 from
    
Selenium import webdriver
import time
    
browser = Webdriver. Firefox ()
    
browser.get ("http://www.baidu.com")
Time.sleep (2)
    
print "Browser maximization"
Browser.maximize_ Window ()  #将浏览器最大化显示
time.sleep (2)
    
browser.find_element_by_id ("kw"). Send_keys ("Selenium")
browser.find_element_by_id ("su"). Click ()
time.sleep (3)
browser.quit ()

Set browser fixed width, height

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Maximize or not flexible, can you set the browsing width, high display? Of course it's OK.

#coding =utf-8 from
Selenium import webdriver
import time
    
browser = Webdriver. Firefox ()
    
browser.get ("http://m.mail.10086.cn")
Time.sleep (2)
    
print "Set browser width 480, high 800 display"
Browser.set_window_size  #参数数字为像素点
time.sleep (3)
browser.quit ()

This demand is still there, for example, when we visit the mobile Web site through the PC Browser, adjust the browser for the phone screen wide, high, easy to find some display problems. (Above the mobile phone mail site is the author tested a product)

Manipulating browsers to move forward and back

The browser has a backward, forward button, for browsing the Web site is more convenient for the students to do the Web Automation test should be a more difficult to simulate the problem; in fact, it's very simple, look at how Python is implemented

#coding =utf-8 from
    
Selenium import webdriver
import time
    
browser = Webdriver. Firefox ()
    
#访问百度首页
first_url= ' http://www.baidu.com '
print ' Now access%s '% (first_url)
Browser.get ( First_url)
Time.sleep (2)
    
#访问新闻页面
second_url= ' http://news.baidu.com '
print ' Now access%s '% ( Second_url)
browser.get (second_url)
time.sleep (2)
    
#返回 (backward) to Baidu home page
print "Back to  %s"% ( First_url)
browser.back ()
time.sleep (1)
    
#前进到新闻页
print "forward to  %s"% (Second_url)
Browser.forward ()
time.sleep (2)
    
browser.quit ()

To make the process clearer to you, print and sleep are added to each step.

To tell the truth, these two functions are not commonly used, can think of the scene is a few pages to jump back and forth, but do not want to use the case of Get URL.

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.