Selenium (2): Operation input box/button method, selenium input box

Source: Internet
Author: User

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

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.