Selenium2+python Automation 34-get Baidu input Lenovo Word

Source: Internet
Author: User
Tags lenovo

Objective

Recently, a small partner asked Baidu input, the input box below the Lenovo word how to locate, this is not very difficult, with the above-mentioned element positioning can be located completely.

This article to Baidu input box input keyword match, print out the associative words.

I. Positioning input box associative words

1. First enter the keywords in the Baidu input box, such as: blog, and then enter the box below will automatically match the keywords.

2. At this time can be used firebug tools to locate the word Lenovo, you can see the words below match out have a common class attribute, then you can all locate.

Second, print all the matching words

1. Obtaining text information via the Get_attribute () method

Third, click on one of the

1. Click on one of the associated words, such as: the second

2. Here you can first add a judgment, if you get to the click, did not get to the click, so as not to throw an exception.

(If you want to click, use the For loop)

Third, reference code

# Coding:utf-8
From selenium import Webdriver
Import time
Driver = Webdriver. Firefox ()
Driver.implicitly_wait (10)
Driver.get ("http://www.baidu.com")
Time.sleep (1)
driver.find_element_by_id ("kw"). Send_keys (U "blog")
# Get the Baidu input box
Time.sleep (1)
BD = Driver.find_elements_by_class_name ("Bdsug-overflow")
For I in BD:
Print I.get_attribute ("Data-key")

# Click on one of them, such as: the second one
If Len (BD) > 1:
Bd[1].click ()
# Print the current page URL
Print Driver.current_url
Else
Print "Not getting matching words"

Selenium2+python Automation 34-get Baidu input Lenovo Word

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.