Home >Backend Development >Python Tutorial >How to Extract Text with Selenium WebDriver in Python Without Relying Solely on XPath?
Getting Text with Selenium WebDriver in Python
When attempting to obtain text using Selenium WebDriver, you may encounter issues if you rely solely on XPath, especially if the ID is subject to change. To address this challenge, consider employing the following approach:
<code class="python">text = driver.find_element_by_class_name("current-stage").text</code>
As you can observe, the getText() method is replaced with just .text. This modification retrieves the text content within the targeted element.
To ensure accuracy, it's advisable to verify the extracted text after retrieval. Avoid passing in expectations about the text's content, as this may lead to unforeseen errors. By adhering to these suggestions, you can reliably gather text using Selenium WebDriver.
The above is the detailed content of How to Extract Text with Selenium WebDriver in Python Without Relying Solely on XPath?. For more information, please follow other related articles on the PHP Chinese website!