This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging
🐻4. Action chain:
👑 (1)
-
In addition to simple click actions, there are also some slightly more complex actions in Selenium, which need to use the ActionChains submodule to meet our needs.
-
ActionChains can complete a bit more complex page interaction, such as element drag and drop, mouse movement, hover behavior, content menu interaction. Perform () ¶ Perform (); perform(); perform(); perform(); perform();
-
Import the ActionChains package:
from selenium.webdriver.common.action_chains import ActionChains
👑 (2) Methods:
Methods provided by ActionChains | role |
---|---|
click(on_element=None) | Click the incoming element with the left mouse button |
double_click(on_element=None) | Double click the left mouse button |
context_click(on_element=None) | Right click |
click_and_hold(on_element=None) | Click the left mouse button and hold it down |
release(on_element=None) | Release the left mouse button at an element |
drag_and_drop(source, target) | Drag to an element and release |
drag_and_drop_by_offset(source, xoffset, yoffset) | Drag it to a point and release it |
move_to_element(to_element) | The mouse moves over an element |
move_by_offset(xoffset, yoffset) | Move the mouse to the specified x, y position |
move_to_element_with_offset(to_element, xoffset, yoffset) | Moves the mouse to a distance from an element |
perform() | Perform all actions in the chain |
👑 (3) Example:
Example:
- Package: guide from selenium.webdriver.com mon. Action_chains import ActionChains
- Instantiate ActionChains object: Action=ActionChains(driver)
- Element = action.context_click (username)
- Execution: element. The perform ()
🐮5. Extract the text content and attribute values of the node:
♥️ (1) Obtain text content:
- element.text
Gets the text content by locating the text property of the obtained tag object.
♥️ (2) Obtain the attribute value:
- Element.get_attribute (‘ attribute name ‘)
Get the value of the attribute by locating the get_attribute() function of the obtained tag object and passing in the attribute name.
🐒6. Execute JavaScript code:
For some operations: Selenium does not provide an API. For example: scroll down the page, but Selenium’s great creators have given us an even more convenient way to run JavaScript directly, using the execute_script() method! |
⚽️ Actual combat demonstration:
📌①
import time
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://baike.baidu.com/item/%E7%99%BE%E5%BA%A6%E6%96%87%E5%BA%93/4928294?fr=aladdin')
# Execute JS code and slide the page to the bottom!
js = 'window.scrollTo(0, document.body.scrollHeight)'
browser.execute_script(js)
# Execute JS code, pop-up prompt text!
browser.execute_script('alert(" We're at the bottom!" ) ')
time.sleep(3)
Copy the code
🐫7. Switch between tabs/Windows:
💊 (1) Methods:
If you need to open a new page when you use Selenium to operate the browser, there will be problems at this time. Since we use Selenium to operate the first open window, we cannot operate the newly opened page, so we need to use the toggle window — that is, the handle toggle method!
methods | role |
---|---|
js = ‘window.open(“www.baidu.com”); ‘chrome.execute_script(js) | Open a new TAB |
window_handles | Gets handles to all page Windows |
current_window_handle | Gets a handle to the current page window |
switch_to.window(window_name) | Locate the page go to the specified window_name page |
Note: the order of Window_handles is not the order of the tabs on the browser. Try to avoid multiple tabs!
💊 (2) Actual combat demonstration:
🔆
Window toggle: First get the window handle for all tabs; Then use the window handle to switch to the TAB that the handle points to. Window handle: refers to the identifier pointing to the TAB object! 解 决 : #1. Get a list of current_windows = driver.window_handles for the current TAB
Driver.switch_to. window(Windows [0])
🔆② Above code:
import time
from selenium import webdriver
driver=webdriver.Chrome()
driver.get('https://www.baidu.com/')
time.sleep(1)
driver.find_element_by_id('kw').send_keys('python')
time.sleep(1)
driver.find_element_by_id('su').click()
time.sleep(1)
Open a new TAB by executing js
js = "window.open('https://www.sougou.com');"
driver.execute_script(js)
time.sleep(1)
# 1. Get all the current Windows
windows = driver.window_handles
time.sleep(2)
# 2. Switch by window index
driver.switch_to.window(windows[0])
time.sleep(2)
driver.switch_to.window(windows[1])
time.sleep(6)
driver.quit()
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! |
This blogger will continue to update the basic crawler column and actual crawler column, carefully read this article friends, you can like the collection and comment out of your reading feeling. 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 obtain my consent, and annotate the source and the name of this blogger, thank you!