Selenium (2): Operation input box/button method, selenium input box
A web page must contain both input boxes and buttons. How can I use Selenium in Python to operate the input boxes and buttons on a web page? This article provides you with a simple introduction.
This article uses an example to show how to use Selenium to open a Baidu webpage, search for the keyword "Python", and perform the search action. The Code is as follows:
Python
# Autor: 5bug # WebSite: http://www.XuePython.wang # Learn Python network QQ group: 643829693 from selenium import webdriverdriver = webdriver. chrome ("C:/Users/5bug/AppData/Local/Google/Chrome/Application/chromedriver.exe") # driver. maximize_window () driver. get ('https: // www.baidu.com ') input = driver. find_element_by_id ('kw ') input. send_keys ('python') button = driver. find_element_by_xpath ('// * [@ id = "su"]') print (button. get_attribute ("value") button. click ()
Running effect:
The following methods are used in this example:
Find_element_by_id: finds the element object through the element id. The parameter is the element id.
Find_element_by_xpath: Uses xpath to find element objects. The parameter is element xpath.
Send_keys: sends text to the input box. The parameter is text content.
Get_attribute: gets an attribute of an element. The parameter is the attribute name.
The text input box also uses the clear () method to clear the current text.
This article first in learning Python Network: http://www.XuePython.wang