当前位置:网站首页>Selenium advanced operations

Selenium advanced operations

2022-07-06 19:43:00 Cold Lane (* _*)

No interface mode

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get('https://www.baidu.com/')

perform js Code , Pull the page to the bottom

driver.execute_script(
    'window.scrollTo(0, document.body.scrollHeight)'
)

Simulate keyboard keys

#  Enter cold Lane in the search box 
dirver.find_element(By.ID, 'kw').send_keys(' deserted alleyways ')
#  Enter a space 
dirver.find_element(By.ID, 'kw').send_keys(Keys.SPACE)
# ctrl+a  Simulate select all 
dirver.find_element(By.ID, 'kw').send_keys(Keys.CANCEL,'a')
# ctrl+c  Simulate replication 
dirver.find_element(By.ID, 'kw').send_keys(Keys.CANCEL,'c')
# ctrl+v  Simulate paste 
dirver.find_element(By.ID, 'kw').send_keys(Keys.CANCEL,'v')
#  Simulated carriage return 
dirver.find_element(By.ID, 'kw').send_keys(Keys.ENTER)

Simulation mouse

Import mouse event class

from selenium.webdriver import ActionChains

Instantiate mouse events

ActionChains(dirver)

Specify mouse behavior

move_to_element()

Execute mouse behavior

perform()

Here is an example

I want to click Advanced Search

Import

 

from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium import webdriver

Open the browser

dirver = webdriver.Chrome()
dirver.get('https://www.baidu.com/')

Find the location of the setting

 

Advanced search location has also been found

 

#  Move to settings 
set_node = dirver.find_element(By.XPATH, '//*[@id="s-usersetting-top"]')
ActionChains(dirver).move_to_element(to_element=set_node).perform()
#  Find the advanced search node , And click the 
dirver.find_element(By.LINK_TEXT, ' Advanced search ').click()

Run it to see the effect

a !

Switch handle  

Use URL type

Open the link on the page and a new window appears , But browser objects dirver Or the object of the previous page , You need to switch to a different window mirroring operation

Application plan    dirver.switch_to.window()

 Get all current handles first ( list )
 Then switch to the specified handle ( utilize )
all_handles = dirver.window_handles
dirver.switch_to.window(all_handles[1])  # 1 Is the second tab 

What is the handle ?

Simply put

  I click    Ice and snow as the medium An appointment to the Winter Olympics   after , There is one more label in the label column above , This is the handle .

selenium frame

characteristic

Web pages are nested in web pages , Switch to frame, Then perform other operations

Processing steps

Switch to the... To be processed frame

stay frame Locate and operate page elements in

Returns the currently processed frame It is the upper level page or main page

Common methods

Switch to frame: driver.switch_to.frame(frame Node object )

Return to upper level :driver.switch_to.parent_frame()

Go back to the main page ;driver.switch_to.default_content()

Instructions

The default support id and name Attribute value lookup :switch_to.frame(id|name)

First find frame node :frame_node = driver.find_element_by_

Switch to frame:dirver.switch_to.frame(frame_node)

原网站

版权声明
本文为[Cold Lane (* _*)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131235471584.html