Selenium Webdriver (Python) API

Source: Internet
Author: User
Tags tag name xpath

1.
Introduction to Selenium-webdriver by Example
A simple way to get started is this example,
It searches for the term "Cheese" on Google,
The title of the resulting page is then output to the console.

Java CSharp python
From selenium import Webdriver
From selenium.common.exceptions import timeoutexception
From Selenium.webdriver.support.ui import webdriverwait
# but since 2.4.0
From Selenium.webdriver.support import expected_conditions as EC
# but since 2.26.0

Driver = Webdriver. Firefox ()
# Create a new instance of Firefox driver

Driver.get ("https://www.google.com")
# Go to Google homepage

Print Driver.title
# page Ajaxy So the title turns out to be this:

Inputelement = Driver.find_element_by_name ("q")
# The name attribute of the found element is Q (google search box)

Inputelement.send_keys ("cheese!")
# type Search

Inputelement.submit ()
# Submit form (although Google Auto Search is not submitted now)

Try
Webdriverwait (Driver). Until (Ec.title_contains ("cheese!"))
# We have to wait for the page to refresh, the last thing that seems to be updated is the title

Print Driver.title
# you should see "cheese! -Google Search "

Finally
Driver.quit ()

2.
Crawl page
The first thing you might want to do with Webdriver is to navigate to a page.
The normal practice is to call "get":

Driver.get ("https://www.google.com")

3.
Find UI elements (webelements)
The "Find" method uses a locator or query object named "by".
The "by" policy is listed below.

by ID

An example of how to find an element such as the following:

<div id= "Coolestwidgetevah" >...</div>

element = driver.find_element_by_id ("Coolestwidgetevah")

Or

From selenium.webdriver.common.by Import by
element = Driver.find_element (by=by.id, value= "Coolestwidgetevah")

by Class Name

An example of how to find an element such as the following:

<div class= "Cheese" ><span>cheddar</span></div><div class= "cheese" ><span> Gouda</span></div>

cheeses = driver.find_elements_by_class_name ("cheese")

Or

From selenium.webdriver.common.by Import by
cheeses = driver.find_elements (by.class_name, "cheese")

by Tag Name

An example of how to find an element such as the following:

<iframe src= "..." ></iframe>

frame = Driver.find_element_by_tag_name ("iframe")

Or

From selenium.webdriver.common.by Import by
frame = Driver.find_element (By.tag_name, "iframe")

by Name

An example of how to find an element such as the following:

<input name= "Cheese" type= "text"/>

Cheese = driver.find_element_by_name ("cheese")

Or

From selenium.webdriver.common.by Import by
Cheese = driver.find_element (by.name, "cheese")

by Link Text

An example of how to find an element such as the following:

<a href= "Http://www.google.com/search?q=cheese" >cheese</a>>

Cheese = Driver.find_element_by_link_text ("cheese")

Or

From selenium.webdriver.common.by Import by
Cheese = driver.find_element (by.link_text, "cheese")

by Partial Link Text

An example of how to find an element such as the following:

<a href= "Http://www.google.com/search?q=cheese" >search for cheese</a>>

Cheese = Driver.find_element_by_partial_link_text ("cheese")

Or

From selenium.webdriver.common.by Import by
Cheese = driver.find_element (by.partial_link_text, "cheese")

by CSS

Here's an example of cheese:

<div id= "Food" ><span class= "Dairy" >milk</span><span class= "dairy aged" >cheese</span> </div>

Cheese = Driver.find_element_by_css_selector ("#food span.dairy.aged")

Or

From selenium.webdriver.common.by Import by
Cheese = driver.find_element (by.css_selector, "#food span.dairy.aged")

by XPath

This is a small abstraction, so for the following paragraph of HTML:

<input type= "text" name= "Example"/>
<input type= "text" name= "other"/>

Inputs = Driver.find_elements_by_xpath ("//input")

Or

From selenium.webdriver.common.by Import by
Inputs = Driver.find_elements (By.xpath, "//input")

Using JavaScript

A simple example on a page loaded with jquery:

element = Driver.execute_script ("Return $ ('. Cheese ') [0]")

Find all the input elements for each label on the page:

Labels = driver.find_elements_by_tag_name ("label")
Inputs = Driver.execute_script (
"var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++) {"+
"Inputs.push (document.getElementById (Labels[i].getattribute (' for"));} return inputs; ", labels)

Selenium Webdriver (Python) API

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.