This article will give you an in-depth introduction to the Web's automated testing tool Selenium [recommended collection]

Preface

In order to consolidate the knowledge learned, the author tried to start publishing some study note blogs to facilitate future review. Of course, it would also be great if it could help some newbies learn new technologies. The author is a rookie. If there are recording errors in the article, readers and friends are welcome to criticize and correct them.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, you are welcome to ask me in the comment area)

discover treasure

A few days ago I discovered a giant artificial intelligence learning website. It is easy to understand and humorous. I couldn’t help but share it with everyone. 【Treasure Entrance】.

Section 01 Selenium Overview

Selenium is aWeb automated testing tool, originally developed for website automation testing , Selenium can run directly
on the browser, it supports all major browsers

Because Selenium can control the browser to send requests and obtain web page data, it can be applied to the crawler field.

Selenium can let the browser automatically load the page according to our instructions, obtain the required data, and even take screenshots of the page, or determine whether certain actions on the website have occurred

Selenium does not have its own browser and does not support browser functions. Itneeds to be combined with a third-party browser to use

The corresponding method names will be different depending on the version of the Selenium library.

Official documentation:http://selenium-python.readthedocs.io/index.html

Section 02 Install browser driver (take Google as an example)

1. Confirm browser version

Insert image description here

2. Download the corresponding version of the driver

Insert image description here

Insert image description here

3. How to download historical versions of chorm

  • Get browser version number

Visithttps://vikyd.github.io/download-chromium-history-version/, you may need to access the scientific Internet (dddd) , then select the platform you want in the version drop-down, and then enter the version number in the input box, for example: windows 64-bit version 113.0

Insert image description here

  • Select browser version and download the compressed package
    Insert image description here
  • After downloading, unzip and use

Insert image description here

  • Save chromedriver.exe to any location and save the current path to the environment variable (My Computer>>Right-click Properties>>Advanced System Settings>>Advanced>>Environment Variables>> System Variables>Path), be careful not to overwrite the path variable when adding it. If it is overwritten, do not shut down, and then use Baidu. After the addition is successful, use the following code to test.
# 导入 webdriver
import time
from selenium import webdriver

# 调用环境变量指定的PhantomJS浏览器创建浏览器对象
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
# get方法会一直等到页面被完全加载,然后才会继续程序,通常测试会在这里选择
driver.get("https://www.baidu.com/")

# id="kw"是百度搜索输入框,输入字符串"长城"
driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("长城")
# id="su"是百度搜索按钮,click() 是模拟点击
driver.find_element(By.CSS_SELECTOR,"#su").click()

time.sleep(20)

Insert image description here

Section 03 Positioning page elements

定位一个元素用 element,定位一组元素用 elements

1. Open the specified page

1. Do not switch to new window

To use Selenium to open the specified page, you first need to initialize an instance of WebDriver and then use this instance to open the target page. Here is an example using Python and Selenium to show how to open a specified page:

from selenium import webdriver

# 初始化一个WebDriver实例,这里使用Chrome作为浏览器
driver = webdriver.Chrome()

# 打开指定的页面,将URL替换为你要访问的网页地址
url = "https://www.example.com"
driver.get(url)

# 在这里,你可以执行与打开页面相关的操作

# 最后,关闭浏览器窗口
driver.quit()

In the above code, we first import the module of Selenium and initialize a browser Example. Then, use the get method to open the specified page, and replace of the target page with the address of the web page you want to visit. Afterwards, you can perform actions related to your test or task on the page and then close the browser window using the methodwebdriverChromeWebDriverURLquit

2. Switch to new window

The above method will open a new page in the current window or tab, replace the current page with a new URL, usewindow.open(url) Open new page:

driver.execute_script("window.open('https://www.example.com', '_blank');")  # 使用JavaScript打开一个新页面

This method uses the execute_script method to execute the JavaScript code and open the specified parameter tells the browser to open in a new window or tab. This method is suitable for opening the page in a new window without replacing the current pageURL'_blank'URL

Please note that if you use the window.open() method to open a new page, you may need to use driver.window_handles and to manage switching between different windows, just like the window handle mentioned later. This allows you to perform actions in different browser windowsdriver.switch_to.window()

In summary, the execute_script method is typically used in conjunction with other Selenium methods to execute JavaScript for specific interactions and operations, but is not used by itself to open a new page

3. Keep the browser window open

By default, Selenium WebDriver closes the browser window after script execution ends, but by setting the option "detach" True, you can keep the browser window open to perform manual operations

from selenium import webdriver

# 创建ChromeOptions对象
options = webdriver.ChromeOptions()

# 添加选项,防止浏览器自动关闭
options.add_experimental_option("detach", True)

# 初始化一个WebDriver实例,将选项传递给Chrome
driver = webdriver.Chrome(options=options)

# 打开指定的页面
url = "https://www.csdn.net"
driver.get(url)

# 在这里,你可以执行与打开页面相关的操作

# 手动关闭浏览器时,可以保持它打开

2. ID positioning

To target by ID of an element using Selenium, you can use the By.ID selector and find_element method. Here's an example of how to target by the ID of an element:

from selenium import webdriver

# 初始化一个WebDriver实例,这里使用Chrome作为浏览器
driver = webdriver.Chrome()

# 打开指定的页面
url = "https://www.example.com"
driver.get(url)

# 通过元素的ID进行定位并执行操作
element = driver.find_element(By.ID, "element_id")

# 在这里,你可以执行与该元素相关的操作,例如单击、输入文本等
element.click()

# 最后,关闭浏览器窗口
driver.quit()

In the above example, driver.find_element(By.ID, "element_id") locates the element by its ID attribute and stores it in the variable element , which can then be executed with Element-related operations. Remember to replace "element_id" with the actual ID of the element you are looking for.

3. name positioning

# 通过元素的name属性进行定位并执行操作
element = driver.find_element(By.NAME,"element_name")

4. class positioning

# 通过元素的CSS类名进行定位并执行操作
element = driver.find_element(By.By.CLASS_NAME, ".element_class")

5. tag positioning

Each tag is often used to define a type of function, so the success rate of identifying an element through tags is very low. Each page generally uses many of the same tags.

# 通过元素的标签名进行定位并执行操作
element = driver.find_element(By.TAG_NAME,"element_tag")

element_tag in the sample code represents the HTML tag name of the element you are looking for, such as < div >, < a >, < p >, etc. Specifically, if you want to find a < div > element, you can replace element_tag with "div" , if you want to find a < /span>, and so on with < a > element, you can replace element_tag"a"

6. xpath positioning

XPath (XML Path Language) is a language for locating elements in XML documents. It also applies to HTML documents because HTML is a variant of an XML-based markup language. XPath is a powerful method of locating elements that allows you to accurately locate elements on a web page,regardless of their positionin the document. Here are some examples of XPath positioning:

1. Positioning by element name:

  • OrientationPossessive connectionElement://a
  • OrientationPossessive paragraphElement://p
  • PositioningFirst titleElement://h1

2. Positioning through element attributes:

  • Locatean element with a specific id attribute:

    //*[@id='element_id']
    
  • Targetingelements with a specific class attribute:

    //*[@class='element_class']
    

3. Positioning through text content:

  • TargetingElements containing specific text:

    //*[text()='要查找的文本']
    
  • Locate elements that beginwith specific text:

    //*[starts-with(text(), '开头文本')]
    
  • TargetingLinks containing specific text:

    //a[contains(text(), '链接文本')]
    

4. Positioning via element hierarchy:

  • PositioningChild element of parent element:

    //div[@id='parent_id']/p(查找 id 属性为 'parent_id' 的 < div > 元素下的所有 < p > 元素)
    
  • PositioningChild elements of ancestor elements:

    //div[@class='grandparent_class']//span(查找 class 属性为 'grandparent_class' 的祖先元素下的所有< span >元素)
    

5. Use logical operators:

  • Positionelements that meet multiple conditions at the same time:

    //input[@type='text' and @name='username'](查找type属性为 'text' 且 name 属性为 'username' 的输入框)
    

These are just some basic examples of XPath. XPath has a very rich syntax and functionality. You can combine and customize different conditions to locate elements according to your needs. In Selenium, you can use the find_element + By.XPATH method to achieve XPath positioning, for example:

element = driver.find_element(By.XPATH,"//a[contains(text(), '链接文本')]")

This will find link elements that contain specific text, you can modify the XPath expression to target different elements as needed.

6. Example

<html>
  <head>...<head/>
  <body>
    <div id="csdn-toolbar">
      <div class="toolbar-inside">
        <div class="toolbar-container">
          <div class="toolbar-container-left">...</div>
          <div class="toolbar-container-middle">
            <div class="toolbar-search onlySearch">
			<div class="toolbar-search-container">
				<input id="toolbar-search-input" autocomplete="off" type="text" value="" placeholder="C++难在哪里?">

According to the above tag, you need to position the last rowinput tag. Four methods are listed below.xpath The positioning methods are diverse and not unique. Use It can be analyzed according to the situation.

# 绝对路径(层级关系)定位
driver.find_element(By.XPATH,
	"/html/body/div/div/div/div[2]/div/div/input[1]")
# 利用元素属性定位
driver.find_element(By.XPATH,
	"//*[@id='toolbar-search-input']"))
# 层级+元素属性定位
driver.find_element(By.XPATH,
	"//div[@id='csdn-toolbar']/div/div/div[2]/div/div/input[1]")
# 逻辑运算符定位
driver.find_element(By.XPATH,
	"//*[@id='toolbar-search-input' and @autocomplete='off']")

7. css selector

CSS uses selectors to bind attributes to page elements. It can flexibly select any attribute of the control. Generally, positioning is faster than xpath. Using CSS selectors to position elements is very common and convenient in Selenium. Here are some common CSS selector examples:

1. Positioning by element name:

  • Orientation Possessive conjunction element :a
  • Orientation Possessive paragraph element :p
  • Orientation Possessive random element :button

2. Positioning by element ID:

Targetan element with a specific ID attribute:#element_id

element = driver.find_element(By.CSS_SELECTOR,"#element_id")

3. Locate by class name:

  • Locateelements with a specific class attribute: .element_class
element = driver.find_element(By.CSS_SELECTOR,".element_class")

4. Positioning through element attributes:

  • Locate elements witha specific attribute value:[attribute='value']
element = driver.find_element(By.CSS_SELECTOR,"[name='username']")

5. Partial matching through attribute values:

  • Locate an element containing a specific attribute value: [attribute*=‘value’]
element = driver.find_element(By.CSS_SELECTOR,"[href*='example.com']")

6. By combining conditions:

  • Positionelements that meet multiple conditions at the same time: .class1.class2
element = driver.find_element(By.CSS_SELECTOR,".element_class1.element_class2")

7. Positioning of child elements:

  • Positioningchild element of parent element:#parent_id > .child_class
element = driver.find_element(By.CSS_SELECTOR,"#parent_id > .child_class")

8. Pseudo-class selector:

For example, positioningmouse-hover element: :hover

element = driver.find_element(By.CSS_SELECTOR,"a:hover")

8. link positioning

Locating hyperlink (link) elements using Selenium usually involves looking for elements with the <a> tag, which is the link tag in HTML. You can use different methods to locate hyperlinks, such as by text content, link text, partial link text, etc. Here are some common examples of link targeting:

1. Target by link text (exact match):

Use the text content of the link for targeting, making sure the text matches the link exactly:

   # 查找所有链接文本为"下一页"的元素
   element = driver.find_elements(By.LINK_TEXT, "文本")

2. Targeting by link text (partial match):

Targeting using part of the link's text content can match part of the link's text:

   element = driver.find_element(By.PARTIAL_LINK_TEXT,"部分文本")

For example, if your link text is "Click here for more information," you can use "Click here" or "Get more information" for partial matching.

These methods are very convenient, especially when you know the text content of the link. Note, however, that they are case-sensitive, so make sure the case of the text content matches the link text.

To find multiple links, you can use find_element**s**(By.LINK_TEXT, "文本") or find_element**s**(By.PARTIAL_LINK_TEXT,"部分文本"), which will return a list of elements, and you can iterate through the list to find multiple Link element.

Example:

elements = driver.find_elements(By.PARTIAL_LINK_TEXT,"部分文本")
for element in elements:
    print(element.text)

This will print all link elements that contain "partial text" of link text.

9. Example Youdao Translation

Visit the Youdao Translation website, enter words, and get the translated content

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 创建ChromeOptions对象
options = webdriver.ChromeOptions()

# 添加选项,防止浏览器自动关闭
options.add_experimental_option("detach", True)

# 创建Chrome WebDriver
driver = webdriver.Chrome(options=options)

# 打开有道翻译页面
driver.get("https://fanyi.youdao.com/")

# 等待输入框可见
input_element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "js_fanyi_input"))
)

# 输入内容
input_element.send_keys("hello")


# 如果有广告弹出框,关闭它
try:
    close_btn = driver.find_element(By.CSS_SELECTOR, ".close")
    close_btn.click()
except Exception:
    pass  # 如果没有广告,继续执行

# 等待翻译结果出现
transTarget = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "js_fanyi_output_resultOutput"))
)

# 输出翻译结果
print(transTarget.text)

# 关闭浏览器
# driver.quit()

Insert image description here

Section 04 Browser Control

1. Modify browser window size

webdriver provides set_window_size() method to modify the size of the browser window

from selenium import webdriver

# 初始化浏览器驱动程序,这里使用Chrome作为示例
driver = webdriver.Chrome()

# 最大化浏览器窗口
driver.maximize_window()

# 或者设置特定大小的窗口
# driver.set_window_size(1024, 768)  # 传递所需的宽度和高度

# 访问网页
driver.get("https://www.example.com")

# 在这里执行其他操作...

# 关闭浏览器
driver.quit()

2. Browser forward & back

To perform browser forward and backward operations in Selenium, you can use forward() and back() method

# 在这里执行其他操作...

# 执行前进操作
driver.forward()

# 执行后退操作
driver.back()

# 在这里执行其他操作...

Please note that forward and backward operations often rely on the browser's history. If there are no pages forward or backward in the browser history, these methods may not do anything. Therefore, before using forward and back,make sure that the browser has visited multiple pages to establish a history

3. Browser refresh

To perform browser refresh operation in Selenium, you can use refresh() method. Here is an example showing how to use Selenium in Python to refresh a browser page:

from selenium import webdriver

# 刷新浏览器页面
driver.refresh()

This method will reload the current page as if the user manually clicked the browser's refresh button. The refresh operation can be used to reload page content to ensure that your test script can reload the page when the page state changes

4. Browser window switching

In Selenium, to switch browser windows, you can use thewindow_handles attribute to get the currently openAll window handles, and then use the switch_to.window() method Switch to a specific window handle. Here's an example of how to switch browser windows using Selenium in Python:

from selenium import webdriver

# 初始化浏览器驱动程序,这里使用Chrome作为示例
driver = webdriver.Chrome()

# 打开第一个网页
driver.get("https://www.example1.com")

# 打开第二个网页
driver.execute_script("window.open('https://www.example2.com', '_blank');")

# 获取所有窗口句柄
window_handles = driver.window_handles

# 切换到第二个窗口
driver.switch_to.window(window_handles[1])

# 在第二个窗口执行操作

# 切换回第一个窗口
driver.switch_to.window(window_handles[0])

# 在第一个窗口执行操作

# 关闭浏览器
driver.quit()

In the above example, we first open two different web pages and then use window_handles to get all window handles. We can perform operations in different browser windows by switching to different window handles

Note that window handle indexes usually start at 0, so the first window's handle is window_handles[0] , the second window's handle is window_handles[1] , and so on. You can switch to other window handles to perform operations as needed

Window Handle is an identifier or reference used to uniquely identify a browser window. Whenever you open a new browser window or tab, the browser assigns a unique handle to the window. These handles are assigned at the browser level and are used to identify different browser windows or tabs for switching and manipulation in a multi-window browser environment

In automated testing tools like Selenium, window handles are used to control and switch between different browser windows in order to perform operations between multiple windows. By getting the window handle, you can switch focus from one window to another to perform various operations, such as switching between different windows, operating pop-up windows, etc.

A window handle is usually a string that you can use to locate and manipulate a specific browser window. In Selenium, you can use the window_handles property to get all currently open window handles, and then use the switch_to.window() method to switch to a specific window handle. This makes it easier to automate testing or operations in a multi-window browser environment

5. Common operations

When using Selenium WebDriver or a similar automated testing tool, these methods can be used to interact with elements of the web page and obtain information. Here are usage examples for each method:

1. send_keys(): simulate input of specified content

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://example.com")

# 找到输入框元素并输入文本
input_element = driver.find_element_by_id("username")
input_element.send_keys("myusername")

In this example, the send_keys() method simulates entering "myusername" in the input box with the id "username"

2. clear(): clear text content

# 清除输入框中的文本
input_element.clear()

The clear() method is used to clear the text content in the previous input box

3. is_displayed(): Determine whether the element is visible

# 检查元素是否可见
if input_element.is_displayed():
    print("Input element is visible on the page.")
else:
    print("Input element is not visible on the page.")

The is_displayed() method is used to check whether an element on the page is visible. In this example, if the input box is visible, "Input element is visible on the page." will be printed.

4. get_attribute(): Get the tag attribute value

# 获取元素的href属性值
link_element = driver.find_element_by_link_text("Example Link")
href_value = link_element.get_attribute("href")
print("Href attribute value is:", href_value)

The get_attribute() method is used to get the value of a specified attribute of an element. In this example, it gets the href attribute value of the element whose link text is "Example Link"

5. size: returns the size of the element

# 获取元素的宽度和高度
element_size = input_element.size
print("Element size is:", element_size)

size is a property that returns the width and height of an element. In this example, it gets the size of the input box element

6. text: Returns the element text

# 获取元素的文本内容
paragraph_element = driver.find_element_by_css_selector("p")
paragraph_text = paragraph_element.text
print("Paragraph text is:", paragraph_text)

text is a property that returns the text content of an element. In this example, it gets the text content of the <p> element

6. Example CSDN page element interaction

# coding=utf-8
import time

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

driver = webdriver.Chrome()

driver.get('https://www.csdn.net/')
time.sleep(2)
# 定位搜索输入框
text_label = driver.find_element(By.ID, "toolbar-search-input")

# 在搜索框中输入 东离与糖宝
text_label.send_keys('东离与糖宝')
time.sleep(2)

# 清除搜索框中的内容
text_label.clear()
time.sleep(2)

# 输出搜索框元素是否可见
print(text_label.is_displayed())
# 输出placeholder的值
print(text_label.get_attribute('placeholder'))

# 定位搜索按钮
button = driver.find_element(By.ID, 'toolbar-search-button')
# 输出按钮的大小
print(button.size)
# 输出按钮上的文本
print(button.text)

'''输出内容
True
搜CSDN
{'height': 32, 'width': 88}
搜索
'''

Section 05 Mouse Control

1. Click on the element

左键不需要用到 ActionChains

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get("https://example.com")

element = driver.find_element_by_id("my_element")

# 单击元素
ActionChains(driver).click(element).perform()

2. Double-click the element

# 双击元素
ActionChains(driver).double_click(element).perform()

3. Right click on the element

# 右键单击元素
ActionChains(driver).context_click(element).perform()

4. Hover over the element (mouseover)

模拟悬停的作用一般是为了显示隐藏的下拉框

# 鼠标悬停在元素上
ActionChains(driver).move_to_element(element).perform()

5. Drag the element to another location

# 拖拽元素到目标位置
target_element = driver.find_element_by_id("target_element")
ActionChains(driver).drag_and_drop(element, target_element).perform()

Section 06 Keyboard Control

1. Enter text

Use the send_keys method to enter text into the active element as if the user had typed it manually

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://example.com")

# 定位到文本输入框并输入文本
text_input = driver.find_element_by_id("my_textbox")
text_input.send_keys("Hello, World!")

2.Button

Use the send_keys method to simulate the operation of specific keys, such as the Enter key, the Backspace key, the Tab key, etc.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://example.com")

# 模拟按下回车键
text_input = driver.find_element_by_id("my_textbox")
text_input.send_keys(Keys.ENTER)

3. Key combination

You can useKeys class to simulate key combinations on the keyboard, such as Ctrl+C (copy) and Ctrl+V (paste) .

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://example.com")

# 模拟Ctrl+C(复制)
text_input = driver.find_element_by_id("my_textbox")
text_input.send_keys(Keys.CONTROL, 'c')

# 模拟Ctrl+V(粘贴)
another_text_input = driver.find_element_by_id("another_textbox")
another_text_input.send_keys(Keys.CONTROL, 'v')

4. Other keyboard operations

operate describe
Keys.F1 F1 key
Keys.SPACE space
Keys.TAB Tab key
Keys.ESCAPE ESC
Keys.ALL Alt key
Keys.SHIFT Shift key
Keys.ARROW_DOWN down arrow
Keys.ARROW_LEFT left arrow
Keys.ARROW_RIGHT right arrow
Keys.ARROW_UP up arrow

Section 07 Elements Wait

Most web applications today use Ajax technology. When a page is loaded into the browser, elements within the page can be loaded at different points in time. This makes it difficult to locate the element, and if the element is no longer on the page, an ElementNotVisibleException exception will be thrown. Using waits, we can solve this problem. waits provides the time interval between some operations - mainly positioning an element or any other operation on that element.

Selenium Webdriver provides two types of waits - implicit and explicit. Explicit waiting will cause WebDriver to wait for certain conditions to be met before further execution. The implicit wait allows Webdriver to wait for a certain period of time before searching for an element.

1. Implicit wait

Implicit Wait: Set a global wait time. If Selenium cannot find the element immediately, it will wait for the specified time and try again. This applies to the entire testing process

from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(10)  # 设置等待时间为10秒

driver.get("https://example.com")
element = driver.find_element_by_id("myElement")

2. Show waiting

Explicit Wait: Explicit wait allows you to wait for specific conditions for specific operations. You can wait for the element to be visible, clickable, etc.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://example.com")

# 等待元素可见
element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, "myElement"))
)

# 等待元素可点击
element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "myElement"))
)

3. Customized wait

Custom wait: You can also create custom wait conditions according to your needs. For example, you can wait for an element's text content to equal a specific value.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

def wait_for_element_with_text(driver, by, value, text, timeout=10):
    return WebDriverWait(driver, timeout).until(
        lambda driver: driver.find_element(by, value).text == text
    )

driver = webdriver.Chrome()
driver.get("https://example.com")

# 等待元素文本内容等于特定值
wait_for_element_with_text(driver, By.ID, "myElement", "Hello, World!")

4. Forced waiting

Changing an implicit wait to a forced wait (using time.sleep()) is generally not a good practice as it results in inefficient code and may waste time.

import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://example.com")  # 替换为你要访问的网页URL

# 假设你已经定位到了包含文本的 WebElement 对象
element = driver.find_element_by_css_selector("span.red-packet-icon-mini > span.num")

# 使用强制等待
time.sleep(5)  # 强制等待 5 秒

# 获取 <span> 元素的文本内容
text_content = element.text

# 打印文本内容
print(text_content)

值得一提的是,对于定位不到元素的时候,从耗时方面隐式等待和强制等待没什么区别

Section 08 Switching Operation

When selenium operates a page, it may jump to a new page (open a new tab) by clicking on a link. At this time, selenium is actually still on the previous page, and we need to switch to locate the latest elements on the page.

Window switching requires the use of switch_to.windows() method.

1. Window switching

When selenium operates a page, it may jump to a new page (open a new tab) by clicking on a link. At this time, selenium is actually still on the previous page, and we need to switch to locate the latest elements on the page.

Window switching requires the use of switch_to.windows() method.

from selenium import webdriver

handles = []
driver = webdriver.Chrome()
driver.get('https://blog.csdn.net/')
# 设置隐式等待
driver.implicitly_wait(3)
# 获取当前窗口的句柄
handles.append(driver.current_window_handle)
# 点击 python,进入分类页面
driver.find_element_by_xpath('//*[@id="mainContent"]/aside/div[1]/div').click()
# 切换窗口
driver.switch_to.window(driver.window_handles[-1])
# 获取当前窗口的句柄
handles.append(driver.current_window_handle)

print(handles)
print(driver.window_handles)

Insert image description here
After clicking to jump, the above code uses switch_to to switch windows. window_handlesThe returned handle list is based on the page When sorted by appearance time, the latest opened page must be the last one, so you can use driver.window_handles[-1] + switch_to to jump to the latest opened page.

If there are multiple open windows, how to jump to the previously opened window? If there is indeed a need, then when opening a window, you need to record each windowkey(别名) and value(handle), save it to the dictionary, and then retrieve based on key. handle

2. Form switching

1. iframe switching

Many pages will also be nested with frames/iframe forms. Selenium cannot directly locate such embedded pages. You need to use the switch_to.frame() method to switch the currently operated object to a frame/iframe embedded page.

By default, switch_to.frame() can use the id or name attribute for direct positioning, but if the iframe does not have an id or name, then you need to use xpath for positioning.

   iframe = driver.find_element(By.ID, "my-iframe-id")
   driver.switch_to.frame(iframe)

2. Switch back to default content:

If you want to switch back to the main page after completing the operation in the iframe, you can use the switch_to.default_content() method:

   driver.switch_to.default_content()

These methods can help you switch between windows and iframes in Selenium. Remember, before switching windows or iframes, you need to get the corresponding window handle or iframe element.

3. Examples

Specifically, to find an element located inside an iframe, you need to follow these steps:

1. First you need to find the iframe element itself and then switch to that iframe

2. Inside the iframe, you can use regular methods to find and manipulate elements

Here's an example of how to switch to an iframe and find the elements within it:

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

driver = webdriver.Chrome()
driver.get("https://example.com")

# 找到 iframe 元素,可以根据其 ID、名称或其他属性来定位
iframe = driver.find_element(By.ID, "my-iframe-id")

# 切换到 iframe
driver.switch_to.frame(iframe)

# 在 iframe 中查找元素并执行操作
element_inside_iframe = driver.find_element(By.CLASS_NAME, "element-class")
element_inside_iframe.click()

# 切换回主页面
driver.switch_to.default_content()

In this example, we first found the element via its ID and then switched to < using /span> to switch back to the main page. and perform the operation. Finally, use , then find the element in iframedriver.switch_to.frame()iframeiframedriver.switch_to.default_content()

Remember to make sure you find the iframe element correctly, and after switching to the iframe, use the driver.find_element() method to find the element inside the iframe.

Section 09 Roller Operation

To simulate wheel operations in Selenium, you can use the send_keys method of the ActionChains class to send keys such as Keys.PAGE_DOWN or Keys.SPACE to the page. This will simulate scrolling down the page.

In your example code, you have already created an ActionChains object, so you only need to use the send_keys method in the loop to simulate the wheel operation. Here's how to modify your code to simulate scrolling down:

from selenium.webdriver.common.keys import Keys

# ...

# 计算滚动的时间间隔(以毫秒为单位)
scroll_interval = 500  # 每500毫秒滚动一次

# 创建ActionChains对象
action_chains = ActionChains(driver)

# 模拟滚动
for _ in range(int(scroll_duration * 1000 / scroll_interval)):
    action_chains.send_keys(Keys.PAGE_DOWN).perform()
    time.sleep(scroll_interval / 1000)

This code simulates pressing the Page Down key to scroll the page. You can adjust scroll_interval and scroll_duration as needed to control the speed and duration of scrolling.

Summarize

You are welcome to leave messages, exchange comments and make criticisms. If the article is helpful to you or you think the author's writing is good, you can click to follow, like, collect and support it.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, you are welcome to ask me in the comment area)

Guess you like

Origin blog.csdn.net/HHX_01/article/details/134015413