Selenium Study Notes (smart waiting) and selenium Study Notes

Source: Internet
Author: User

Selenium Study Notes (smart waiting) and selenium Study Notes

The blogger is trying to use selenium to automatically log on to the Baidu homepage.

The element locating method has not been written wrong after repeated attempts, and the cause of the problem is finally found:

The script runs faster than the page loading speed

For example, the Baidu homepage logon example and script have already started searching for the login pop-up window

However, the page is still being loaded, causing program errors.

The bloggers sorted out the smart waiting methods.

First, display wait: that is, when an element of the page appears and times out, an error is thrown.

1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 4 from learn_webdriver import Webdriver 5 from selenium import webdriver 6 from selenium. webdriver. support. ui import WebDriverWait 7 from time import sleep 8 9 browser_chrome = webdriver. chrome (Webdriver. chrome () 10 browser_chrome.get ("http://www.baidu.com") 11 12 browser_chrome.find_element_by_css_selector ("html body div # wrapper div # head div. head_wrapper div # u1. lb "). click () 13 # locate element 14 WebDriverWait (browser_chrome, 10) of the logon button on the Baidu homepage ). until (lambda the_driver: 15 browser_chrome.find_element_by_class_name ("tang-foreground "). is_displayed () 16 browser_chrome.find_element_by_class_name ("tang-foreground "). find_element_by_name ("userName "). send_keys (u "baidu username") 17 # Go To The logon window and then go to the username input box 18 browser_chrome.find_element_by_name ("password "). send_keys (u "baidu password") 19 sleep (2) 20 print browser_chrome.title21 browser_chrome.quit ()

Here we mainly use the WebDriverWait () Class Construction Method until the login pop-up window element is found.

 

The following is the invisible wait: namely, set the wait time, wait for the page to complete a new operation, and throw an error if the operation times out.

1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 4 from learn_webdriver import Webdriver 5 from selenium import webdriver 6 from selenium. webdriver. support. ui import WebDriverWait 7 from time import sleep 8 9 browser_chrome = webdriver. chrome (Webdriver. chrome () 10 browser_chrome.get ("http://www.baidu.com") 11 12 browser_chrome.find_element_by_css_selector ("html body div # wrapper div # head div. head_wrapper div # u1. lb "). click () 13 # locate the login button element 14 browser_chrome.implicitly_wait (30) 15 # Add intelligent wait time 16 div_user = browser_chrome.find_element_by_class_name ("tang-foreground "). find_element_by_name ("userName") 17 div_user.send_keys (u "baidu userName") 18 div_pass = browser_chrome.find_element_by_name ("password "). send_keys (u "baidu password") 19 sleep (2) 20 print browser_chrome.title21 browser_chrome.quit ()

 

The implicitly method intelligently sets the wait time to 30 S, and throws an exception upon timeout.

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.