Verify the Element and proceed to the subsequent tag

I have a list with various elements, each containing its own delete button. If I want to delete a specific element (such as Test 1 or Test 2), I first need to confirm the element and then proceed with deleting it.

<div class="html-replace-container">
<form id="new_supplier_assignment" class="new_supplier_assignment" action="/supplier_assignments" accept-charset="UTF-8" method="post">
<h3 class="supplier-assignment-header progress-active">
<a href="/products/685">Test 1</a>

  -
<a href="/companies/13">Test 2</a>
</h3>
<a class="btn btn-danger btn-sm" data-replace-html-container="true" data-confirm="Are you sure?" style="float: right" rel="nofollow" data-method="delete" href="/supplier_assignments/122?owner=Customer">Delete</a>

So far, I have this:

<tr>
    <td>click</td>
    <td>xpath=//*[@id='new_supplier_assignment']/..//*[contains(text(), 'Test 1')]/../*[contains(text(), 'Test 2')]</td>
    <td></td>
</tr>

I need to locate the

<a class="btn btn-danger ...>
and click on it without using count() or indexof().

Answer №1

To begin, locate an h3 element where the first a tag contains the text "Test 1" and the second a tag contains the text "Test 2". Then, use the following-sibling::a axis to find the delete button.

XPath:

//h3[a[1][text()="Test 1"] and a[2][text()="Test 2"]]/following-sibling::a

If you are not concerned with which a tag is "Test 1" (it could be the first or second), you can omit the indexes [1] and [2]. Try the following XPath:

//h3[a[text()="Test 1"] and a[text()="Test 2"]]/following-sibling::a

Answer №2

If you want to locate an element using text, you can use the following code:

driver.findElement(by.xpath("//a[contains(text(), 'YOUR TEXT')]")).click()

The buttons are identified by their different texts - 'Test 1' and 'Test 2', so you can easily locate and manipulate them based on their text content.

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

Navigating to a pop-up window that is not detectable through the windows handle in Selenium using Python

I unearthed my old code used for scraping data from Scopus. I originally created it while I was still learning programming. Now, there is a pesky window that pops up on the Scopus site which I am unable to detect using windows_handle. Scopus Pop-up window ...

What is the best way to ensure that each thread in a program can utilize the following useragent in sequential order from a text

How can I assign a unique user agent to each thread from my txt file? Every time I try to import my user agents into the Firefox profile, I encounter an error. '''' from selenium import webdriver from getpass import getpass import th ...

What is the reason that utilizing $("input").first() does not function properly on Google Forms?

While practicing with Selenium, I encountered an issue while using this Google form. Specifically, I found that I couldn't select the first input field by using $("input").first() To confirm this issue, I turned to the web browser (Firefox) console t ...

Is it possible to run Selenium tests on a CentOS7 server without a graphical interface, using either Firefox or Chrome drivers?

In my testing environment on my personal computer, I was able to use Selenium without any issues. However, when I run the same tests on a CentOS7 server without a graphical interface, I encounter multiple errors like not being able to find web elements. ...

Why isn't JMeter's JUnit request sampler wait conditions functioning as expected?

Hello, I am currently working on conducting JUnit load tests with JMeter. My approach involves using Selenium WebDriver and executing it with Selenium JUnit testing. The code runs smoothly when tested with JUnit on Selenium WebDriver. However, when attempt ...

Automate the process of uploading images directly from Windows Explorer using a combination of Selenium and AutoIT

I am facing an issue with automating the process of uploading an image from Windows explorer to a TinyMCE component using Selenium. Due to Selenium's limitations in handling OS controls, I have resorted to using AutoIt for this task. However, despite ...

How can I transfer Selenium web scraped information into a CSV file?

In my latest project, I utilized selenium to extract coronavirus data from a table on worldometers.info/coronavirus by writing the following script. from time import sleep from selenium import webdriver class CoronavirusDataScraper: def __init__(self) ...

How can I use Selenium WebDriver in Python 3 to write content within the <p> tag?

Looking to input code into a contenteditable p tag using Selenium WebDriver. <p contenteditable="true" ></p> Attempted solution: browser = webdriver.Chrome() browser.get(url_link) write = browser.find_element_by_tag_name("p['contentedi ...

Utilizing Selenium and XPath to pinpoint a link based on its text content

I'm attempting to utilize XPath in order to locate an element that contains a specific text, but I seem to be encountering difficulties.... WebElement searchItemByText = driver.findElement(By.xpath("//*[@id='popover-search']/div/div/ul/ ...

Trying to execute Python scripts in Selenium IDE as a test suite

I'm facing a couple of issues when trying to run multiple Python test scripts exported by the Selenium IDE Python Remote Control plugin formatter. 1) The browser window automatically closes after a python script is completed. I am using Firefox for m ...

Using Selenium and Python to find an element with the "if not" statement before calling navigator.find_element

Good evening everyone, I am currently working on extracting data from a specific website: The program follows a loop-based logic. Upon opening the website page, the first step is to access the calendar and check for any matches played in the current mon ...

Python's Selenium is having trouble properly displaying the website in its entirety

After attempting the following code to open a website using Selenium in Python: driver = webdriver.Chrome(r"..\chromedriver_win32\chromedriver.exe") driver.get("https://example.com") I encountered an issue where the website o ...

"I seem to be encountering a problem where I am only able to retrieve the

I have a task that involves retrieving search results of users, with each user represented as an object containing attributes like name, lastName, and title. The goal is to create a list of these objects and perform certain actions on them. I have encounte ...

Tips for managing the windows download notification in IE11 with Selenium Webdriver without relying on AutoIT

During testing on IE11, a situation arises where clicking on the download link for a document results in a new blank window opening with save and cancel options. I prefer to click cancel, return to my original window, and continue with validation tasks. ...

What is the process for selenium to interact with the capybara server on Github Actions?

Currently, I am working on a personal project using Ruby on Rails and trying to implement Continuous Integration with Github Actions, Capybara, and Selenium Service. I chose Selenium Service to align it with the docker-compose of my project. Here is the w ...

Guide on extracting data from all the pages on transfermarkt with the help of selenium

from selenium import webdriver import time driver = webdriver.Firefox() f = open("superliga.csv", "w") driver.get("https://www.transfermarkt.com/") f.write("Emri,Pozicioni,Mosha,Cmimi\\n") f = open("su ...

How to install the Selenium Firefox Webdriver Extension

I've encountered a problem with the Selenium Firefox webdriver extension installation when using Selenium. Here is my code snippet: public class SeleniumFix { public static void main(String[] args) { WebDriver ff = new FirefoxDriver() ...

Python 3.6 script utilizing Selenium unable to extract user review dates and ratings on Google Play Store

I am currently engaged in a project to develop a web crawler using Selenium along with Python 3.6 within a Jupyter Notebook. The primary objective is to extract reviews, their respective dates, and ratings of an APP. The target webpage under consideratio ...

Testing the number of web browser instances

Whenever I execute my tests using TestNG, multiple browser instances are launched before the tests begin. This is how my testng.xml file looks: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> ...

Encountering a connection refusal error while trying to utilize the RSelenium chromedriver

Every time I attempt to run the code below, I encounter a persistent issue: require(RSelenium) driver <- rsDriver( browser = c("chrome"), chromever = "83.0.4103.14", port = as.integer(4445L) ) The result includes seve ...