WebDriver methods that Python beginners were looking for first

Introduction

I myself started Python and coding after joining the company in October. I don't know much yet, but as a reminder of my studies, I would like to write about the methods of Webdriver that I repeatedly investigated when I was just starting Python and coding. It's written while looking at the notes from the beginning, so I'd be very happy if you could point out any mistakes!

What is Selenium Web Driver?

A tool for operating a web browser programmatically. You can programmatically perform a series of steps such as opening chrome, entering keywords in the search window, and clicking the search button. This allows you to scrape and test.

The following is required to use.

from selenium import webdriver

What is WebElement?

Refers to the DOM element. DOM is a mechanism to access each element of xml and html (a tag, p tag, etc.). In other words, the DOM element refers to each element displayed on the Web.

Summary of methods that can be used for WebElement

The method call checks if the element can be referenced. If an error occurs, a StaleElementReferenceException will be thrown.

find_element

method
find_element_by_id(id) Search for elements in child elements by ID
find_element_by_class_name(class) Search for elements in child elements by Class
find_element_by_css_selector(css_selector) Find an element within a child element with a CSS selector
find_element_by_tag_name(name) Search for elements in child elements by tag name (h1, tr, etc.)
find_element_by_name(name) Find the element in the child element with the name property
find_element_by_link_text(link_text) Search by link text string
find_element_by_partial_link_text(link_text) Search by part of the text string of the link
find_element_by_xpath(xpath) Search for elements by xpath

find_elements

The search method is basically the same as find_element. ** * Note that unlike find_element, it will be returned as a list type! !! ** ** If you don't note that it will be returned in the list, you can't use the method, why! !! (I became)

method
find_elements_by_id(id) Search for elements in child elements by ID
find_elements_by_class_name(class) Search for elements in child elements by Class
find_elements_by_css_selector(css_selector) Find an element within a child element with a CSS selector
find_elements_by_tag_name(name) Search for elements in child elements by tag name (h1, tr, etc.)
find_elements_by_name(name) Find the element in the child element with the name property
find_elements_by_link_text(link_text) Search by link text string
find_elements_by_partial_link_text(link_text) Search by part of the text string of the link
find_elements_by_xpath(xpath) Search for elements by xpath

The find_element and find_elements you should I use any because there is a plurality of types, there is also talk that, but it is omitted this time. Personally, I don't think you should use xpath or link text ...

click() Click the element. text Gets the text of the element. clear() For elements that allow you to enter text, the text provided is cleared. send_keys(value) Fill in the elements. The following is required to use.

from selenium.webdriver.common.keys import Keys

get_attribute(name) Gets the properties of the specified attribute or element. If the specified attribute does not exist, None will be returned. is_displayed() Is the element displayed? is_selected() Is the element selected? You can get if a radio button or checkbox is selected.

Sample code

This is a sample code as a bonus. I've introduced some of them, so I haven't used them, but it's not difficult. Please forgive me for omitting it a little.

sample.html


<body>
  <form>
    <h2>name</h2>
      <input id="name" type="text" maxlength="50">
  <h2>Favorite animal</h2>
      <select name="dropdown">
        <option value="elephant">Elephant</option>
        <option value="lion">Lion</option>
        <option value="giraffe">Giraffe</option>
    <label><input type="checkbox" id="chk" checked>Checkbox</label>
    <input type="submit" id="agree_and_goto_next">
  </form>
</body>

sample.py


def test_change_params(self):
    #Enter a name
    name = driver.find_element_by_id('name')
    name.clear()
    name.send_keys('arisa yamamoto')

    #Select Lion from the pull-down menu
    dropdown = Select(driver.find_element_by_id('dropdown'))
    dropdown.select_by_value('1')

    #Check the check box
    chk_box = driver.find_element_by_id('chk')
    if chk_box.is_selected is False:
        chk_box.click()

    #Press the consent button
    agr_btn = driver.find_element_by_id('agree_and_goto_next')
    agr_btn.click()

in conclusion

It sounds easy now, but it turns out that there are too many things that a complete beginner wouldn't know. I will continue to devote myself while taking notes.

Recommended Posts

WebDriver methods that Python beginners were looking for first
python textbook for beginners
Learning flow for Python beginners
Python3 environment construction (for beginners)
Python #function 2 for super beginners
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python #list for super beginners
~ Tips for beginners to Python ③ ~
Python Exercise for Beginners # 2 [for Statement / While Statement]
Python for super beginners Python # dictionary type 1 for super beginners
Python #index for super beginners, slices
<For beginners> python library <For machine learning>
Python #len function for super beginners
See python for the first time
Beginners use Python for web scraping (1)
Run unittests in Python (for beginners)
Beginners use Python for web scraping (4) ―― 1
Looking for location for mercurial python module
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
Nonlocal that Python beginners (junior high school students) were addicted to (sorry for not being able to explain)
INSERT into MySQL with Python [For beginners]
First Steps for Machine Learning (AI) Beginners
Let's put together Python for super beginners
MongoDB for the first time in Python
10 Python errors that are common to beginners
[Python] Read images with OpenCV (for beginners)
WebApi creation with Python (CRUD creation) For beginners
Atcoder standard input set for beginners (python)
[For beginners] Try web scraping with Python
A textbook for beginners made by Python beginners
Data analysis in Python Summary of sources to look at first for beginners
Looking back on the machine learning competition that I worked on for the first time
Selenium + WebDriver (Chrome) + Python | Building environment for scraping
The fastest way for beginners to master Python
Python for super beginners Python for super beginners # Easy to get angry
Causal reasoning and causal search with Python (for beginners)
The first web app created by Python beginners
For new students (Recommended efforts for Python beginners Part 1)
Memo # 3 for Python beginners to read "Detailed Python Grammar"
I tried python programming for the first time.
Memo # 1 for Python beginners to read "Detailed Python Grammar"
Try to calculate RPN in Python (for beginners)
Easy understanding of Python for & arrays (for super beginners)
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Introduction to Programming (Python) TA Tendency for beginners
Memo # 6 for Python beginners to read "Detailed Python Grammar"
How to make Python faster for beginners [numpy]
~ Tips for Python beginners from Pythonista with love ② ~
[Introduction for beginners] Working with MySQL in Python
About Python external module import <For super beginners>
Basic story of inheritance in Python (for beginners)
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver