Locating an HTML element by its inner content using Python's Selenium

I am trying to locate a specific web element using Selenium and Python. The element I am looking for has the following HTML:

<td class="letterbreak first">default</td>

My goal is to locate this element based on its inner HTML text, which is default. Since there are other elements with the same class, I cannot use class-based identification. I have attempted the following approach:

driver.find_element(By.XPATH,"//*[contains(@innerHTML,'default')]")

However, this method results in a NoSuchElementException. Is it possible to locate an element based on innerHTML? If so, how can I achieve this?

Answer №1

Either one of the xapth options can be used.

driver.find_element(By.XPATH,"//*[contains(text(),'default')]")

OR

driver.find_element(By.XPATH,"//*[text()='default']")

To prevent synchronization issues, implement WebDriverWait() and wait for visibility_of_element_located() using the following xpath

WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[contains(text(),'default')]")))

The following libraries need to be imported.

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tkinter becomes unresponsive when running Selenium tasks

It's my first time delving into Tkinter. I decided to write a Python script for saving my pictures from Facebook. I wanted to create a GUI using Tkinker, but encountered an issue where the window freezes when clicking the start button. The process st ...

Extracting content with Python and Selenium: A comprehensive guide

Regarding the following HTML: https://i.stack.imgur.com/7Jddo.png I am trying to use Selenium to select the paragraphs located on the left side of the screen. I attempted using class_name and id, but encountered a NoSuchElementException. Why is this erro ...

Effortless Ways to Automatically Accept SSL Certificates in Chrome

It has been quite some time that I have been attempting to find a way to automatically accept SSL certificates. Unfortunately, I haven't had any success yet. The scenario is this: I am working on selenium tests and every time I run the test on Chrome, ...

Unable to access SVG element in a loop and retrieve all the contents

My goal is to extract job titles from a job-site by scraping the information. I want to navigate to the subsequent pages, extract the data, and continue until no more pages are available. However, when attempting to click on the next page marked as an svg ...

Using iedriverserver.exe to manage Edge in IE Mode with VBA scripting

Are there any code examples available for controlling Edge in IE Mode using iedriverserver.exe in VBA? I have the exe downloaded but am having trouble adding the necessary library to tools->reference in my VBA project. If anyone has experience with this t ...

Repairing the scientific notation in a JSON file to convert it to a floating

I'm currently facing a challenge with converting the output format of certain keys in a JSON file from scientific notation to float values within the JSON dictionary. For instance, I want to change this: {'message': '', &ap ...

Problem encountered when integrating Jenkins with Maven

Encountering a PluginResolutionException while running code through Jenkins. The code works fine in Eclipse, but throws an exception when executed in Jenkins. public class LoginPageTest extends TestBase { // Code snippet here } This is the content of t ...

ChromeDriver fails to open a new page when using the chrome_options parameter

When attempting to open a new page using ChromeDriver, I am using the following code: import selenium from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as E ...

Using Python to terminate a process results in a "Permission denied" error

Currently, I have a python script set up to manage the tcpdump tool. The issue arises when attempting to terminate the tcpdump process within my python script: import subprocess pid = 9669 # pid of the tcpdump process subprocess.call(["sudo", &q ...

Obtain the title of a Youtube video with Selenium in C#

Hey there, I'm currently working on using C# Selenium to extract the title of a YouTube video. Unfortunately, the methods I've tried so far haven't been successful. I've already tested getting the text from other elements and it works f ...

Choosing a row within a table and then opting for a particular radio button positioned beside it

My challenge is selecting a specific row in a table on a website along with the corresponding button next to it. The issue arises when trying to select the button unique to each row, instead of simply remembering the last button position as it currently do ...

Exploring the Contrast Between "Wait until Visible" and "Wait until Located" Commands in Selenium

When utilizing both wait(until.elementLocated(element, timeout)) and wait(until.elementVisible(element, timeout)), I have noticed that the 'wait until visible' method fails in certain scenarios where the 'wait until located' method does ...

Strange and unpredictable shaking phenomenon observed in webdriver with IE9

Experiencing a strange issue while running my script on IE9. On the main page, there are icons/links that redirect to different applications. However, upon opening the main page, I notice that the mouse cursor quickly "shakes" between these links for abo ...

Issue encountered with geckodriver: Unexpected argument '--websocket-port' was detected

I recently updated my Selenium project using the Bonigarcia autodownload webdriver project to the latest versions. I upgraded to Selenium 4.0.0 from Maven repo and also updated the Bonigarcia project to version 5.0.3. However, now when I try to run my test ...

What is the method to retrieve a string from a specific element in Selenium using Python?

I'm facing a challenge in extracting a specific string from the following HTML element: <textarea class="FormTextArea__textarea form-control" aria-label="Photo Caption" aria-describedby="photo-caption__help" id="photo-caption" rows="3">Exterior ...

Launching Microsoft Edge with elevated privileges (scenario: triggering through Selenium)

Having trouble opening Ms Edge in Elevated Mode on Windows 11. Even manual attempts to open it with the Elevated option turned on have failed. Here is an image showing the status of Edge in Elevated Mode: I've experimented using different options t ...

Exploring the Differences Between Selenium-Webdriver with Java and TestNG versus Ruby

Currently, I have an automation suite using selenium-webdriver in Ruby with TestUnit. However, I have noticed that Java offers TestNg, which comes with many additional features. I am considering switching all my suites to Java and TestNg, but I also wond ...

Switch to the browser launched by clicking on a button using WebDriver

As I work with Selenium Webdriver in JAVA, I am attempting to automate a task that involves opening a new tab to execute some operations and then returning to the previous tab (Father). However, this process results in losing the session and encountering ...

Tips for grabbing the following element locator path with Selenium?

I am working on a piece of code to download a CSV file. Here is the code snippet: chromedriver = "/usr/lib/chromium-browser/chromedriver" driver = webdriver.Chrome(chromedriver) driver.get('https://www.aemo.com.au/Electricity/National-Electricity-Mar ...

Could not connect the chrome node to the hub

Below is my docker-compose.yml file that binds Chrome and Firefox nodes to a Hub. version: "3" services: selenium-hub: image: selenium/hub container_name: selenium-hub ports: - "4444:4444" chrome: image: selenium/node-chrome-de ...