This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging

🎍 Part 2 — Selenium Advanced operations!

🐱1. Perform common browser operations:

🚩 (1) SAO operation and code implementation:

Browser operations code
Maximize the browser driver.maximize_window()
The refresh driver.refresh()
back driver.back()
forward driver.forward()
Setting the browser size Driver. Set_window_size (300300).
Setting the browser location Driver. Set_window_position (300200).
Close a single browser window or the entire browser if there is only one TAB driver.close()
Close all browser Windows driver.quit()

🚩 (2) Actual combat use:

Visit three pages in a row — Tmall, Taobao, jingdong, and then call back() to return to the second page — Taobao, and then call forward() to advance to the third page — jingdong!

⚠️①

import time
from selenium import webdriver

browser = webdriver.Chrome()

browser.get('https://www.tmall.com/')
time.sleep(1)
browser.get('https://taobao.com/')
time.sleep(1)
browser.get('https://www.jd.com/')
time.sleep(1)
browser.back()
time.sleep(1)
browser.forward()
time.sleep(1)
browser.close()
Copy the code

🐹2. Element selection/search node:

The first method — find_element(s)by_… Methods:

⚓️ (1) Single node:

There are many different strategies for locating an element on a page. We can choose the most appropriate way to find elements. Selenium provides the following methods:

A single element lookup method role
find_element_by_xpath() Look it up by Xpath
find_element_by_class_name() Through the class attribute
find_element_by_id() By the ID attribute
find_element_by_name() Search through the name attribute
find_element_by_css_selector() Look for syntax rules through CSS selectors
find_element_by_link_text() Find through the link text
find_element_by_partial_link_text() Search by partial match of link text
find_element_by_tag_name() Lookup by tag name (used only if the target element is a unique tag in the current HTML or the first of many located tags!)

Note: With either method, the node type returned is WebElement!

⚓️ (2) Multiple nodes:

The element of the above method plus an S is the lookup method for the corresponding elements.

We can see that the returned content is of type list, and each node in the list is still of type WebElement:

[<selenium.webdriver.remote.webelement.WebElement (session="73974727c0ec09e0b7d57639c3".element="1b33ea80-ba15-91ac-635903f79df2") >, <selenium.webdriver.remote.webelement.WebElement (session="739747be09cb27c0ecd57639c3".element="1ac2f257-4364-be00-84de883b265d") >]Copy the code

Note: an exception is thrown if find_element does not match, but an empty list is returned if find_Elements does not match!

The second method — By object lookup:

In addition to the above multiple search methods, there are two private methods integrated with all the above search methods, let us more convenient use!
methods role
Find_element (By XPATH, ‘/ / button/span) Look one up by Xpath
Find_elements (By XPATH, ‘/ / button/span) Look up multiple by Xpath

The first parameter can choose to use a method to find out, By the way of using XXX XXX, analytic method is as follows (note – By import objects: the from selenium.webdriver.com mon. By the import By) :

  • ID = “id” ​
  • XPATH = “xpath” ​
  • LINK_TEXT = “link text” ​
  • PARTIAL_LINK_TEXT = “partial link text”
  • ​ NAME = “name” ​
  • TAG_NAME = “tag name” ​
  • CLASS_NAME = “class name”
  • ​ CSS_SELECTOR = “css selector”

🐸3. Node interaction:

Selenium can drive the browser to perform some actions, which means you can have the browser simulate some actions.

🚀 (1)

methods role
send_keys() Enter text
clear() Empty words
click() Click on the button
submit() Submit the form

🚀 (2) Example SAO operation:

     # Locate user name
	element=driver.find_element_by_id("userA")
	Enter the user name
	element.send_keys("admin1")
	Delete the entered user name
	element.send_keys(Keys.BACK_SPACE)
	# Re-enter user name
	element.send_keys("admin_new")
	# all
	element.send_keys(Keys.CONTROL,'a')
	# copy
	element.send_keys(Keys.CONTROL,'c')
	# paste
	driver.find_element_by_id('passwordA').send_keys(Keys.CONTROL,'v')
Copy the code

In The End!

Start from now, stick to it, a little progress a day, in the near future, you will thank you for your efforts!

The blogger will continue to update the basic crawler column and actual crawler column (for better page analysis of friends, will also update part of the front-end essential knowledge blog!) If you have read this article carefully, you can like it and comment on it. And can follow this blogger, in the days to read more reptilian!

If there are any mistakes or inappropriate words, please point them out in the comment section. Thank you! If reprint this article, please contact me to explain the meaning and annotate the source and the name of this blogger, thank you!Copy the code