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