乐闻世界logo
搜索文章和话题

Selenium 如何获取写在DOM元素上的文本?

1个答案

1

Retrieving text from DOM elements in Selenium is typically straightforward and simple. Typically, we use Selenium's text attribute to retrieve the text from an element. Here are specific steps and code examples to illustrate how to achieve this:

Step 1: Import necessary libraries

First, ensure Selenium is installed and import the required libraries.

python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys

Step 2: Launch the WebDriver

Next, initialize a WebDriver instance. For example, using Chrome:

python
driver = webdriver.Chrome(executable_path='path_to_chromedriver') driver.get("http://www.example.com")

Step 3: Locate the element you want to retrieve text from

Use Selenium's various locator methods, such as find_element(By.ID), find_element(By.XPATH), or find_element(By.CSS_SELECTOR). For instance, if you know the CSS selector:

python
element = driver.find_element(By.CSS_SELECTOR, "div.content")

Step 4: Retrieve the text of the element

Once you have a reference to the element, use the text attribute to obtain its text content.

python
text = element.text print("Retrieved text:", text)

Example

Suppose a webpage has the following HTML structure:

html
<html> <head><title>Example</title></head> <body> <div class="content">Welcome to my webpage</div> </body> </html>

The corresponding Selenium script is:

python
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome(executable_path='path_to_chromedriver') driver.get("http://www.example.com") element = driver.find_element(By.CLASS_NAME, "content") text = element.text print("Retrieved text:", text) driver.close()

This code outputs:

shell
Retrieved text: Welcome to my webpage

This is the fundamental approach for extracting text from DOM elements in Selenium. Adjust the WebDriver path and target URL according to your specific environment.

2024年8月13日 23:58 回复

你的答案