How to select the text of a span on click in Selenium? Last Updated : 28 Aug, 2024 Comments Improve Suggest changes Like Article Like Report In Selenium, to interact with a span element and get its text, you first need to identify the element. Then, perform a click action on it. After clicking, you can extract the text content of the span. This process helps automate the testing of web pages by simulating user interactions and retrieving data.SetupWe need to install Selenium on our system and a WebDriver, like ChromeDriver, set up properly. Selenium is needed to automate web interactions, and the WebDriver acts as a bridge between your code and the browser, allowing you to control it for testing purposes.pip install seleniumVerify Installation:import seleniumprint(selenium.__version__)Example: Python from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys # Initialize the WebDriver driver = webdriver.Chrome() # or any other WebDriver # Open the target web page driver.get('http://localhost:3000/') # replace with your URL # Locate the span element span_element = driver.find_element(By.XPATH, '//span[@id="mySpan"]') # adjust XPath or selector # Click the span element span_element.click() # Get the text of the span element span_text = span_element.text # Print the text print("Span Text:", span_text) # Close the WebDriver driver.quit() HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Selenium Test</title> </head> <body> <h1>Welcome to Selenium Testing</h1> <p>This is a sample paragraph with a <span id="mySpan">Click me!</span> element.</p> </body> </html> Output:Span Text: Click me!ConclusionSelenium WebDriver lets you interact with web elements and retrieve their text content for testing or data extraction. By automating these tasks, you can efficiently validate web applications and gather information from web pages, making it a valuable tool for ensuring functionality and accuracy in your projects. Comment More infoAdvertise with us Next Article How to select the text of a span on click in Selenium? M manindar_kumar_singh Follow Improve Article Tags : Software Testing Similar Reads How to get text of a tag in selenium - Python? Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. In this article, we will w 1 min read How to get text found between span in Selenium? Selenium WebDriver enables you to interact with web elements on a page. To extract text from a <span> element, you need first to locate it using one of Seleniumâs locator strategies, such as by ID, class, or XPath. After locating the <span> element, you can retrieve its text content with 2 min read How can Selenium select each div separately that have the same class? When working with Selenium, there may be situations where you need to interact with multiple elements on a webpage that share the same class. For example, consider a web page where several div elements have the same class, and you need to perform operations like clicking, retrieving text, or interac 3 min read How to find the src of an image in Selenium java? Selenium WebDriver is a popular automation tool used for testing web applications across different browsers. One common task in web automation is extracting information from web elements, such as retrieving the src attribute of an image. The src attribute holds the URL of the image, and Selenium pro 4 min read How to Click on a Hyperlink Using Java Selenium WebDriver? An open-source tool that is used to automate the browser is known as Selenium. Automation reduces human effort and makes the work comparatively easier. There are numerous circumstances in which the user wants to open a new page or perform a certain action with the click of the hyperlink. In this art 4 min read Like