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!
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
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.
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.
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()
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