Access the designated hyperlink within the table row

Currently experimenting with Selenium Webdriver in Firefox and also looking to test in IE8.

Below is the structure of my HTML:

<table id="table">
<tbody>
    <tr>
        <td>Text1</td>
        <td><a id="assign" href="/assign/1>Assign</a></td>
    </tr>
    <tr>
        <td>Text2</td>
        <td><a id="assign" href="/assign/2>Assign</a></td>
    </tr>
    <tr>
        <td>Text3</td>
        <td><a id="assign" href="/assign/3">Assign</a></td>
    </tr>
</tbody>
</table>

My task at hand is:
To click on the assign link within the row containing Text1.

After some trial and error, I have crafted an XPATH:

//*[@id='table']//tr/td//following-sibling::td//following-sibling::td//following-sibling::td//a
, which successfully targets all the assign links. On modifying it to
//*[@id='table']//tr/td[text='Text1']//following-sibling::td//following-sibling::td//following-sibling::td//a
, Firebug indicates "No matching nodes".

Desiring a CSS selector instead, I attempted

#table>tbody>tr:contains('Text1')
, but received an error message from Firebug stating "Invalid CSS Selector".

Any recommendations?

Answer №1

To locate the td element that comes before a sibling td containing the text "Text1," you can use the following XPath expression to retrieve the corresponding a tag:

//table[@id="table"]//td[preceding-sibling::td="Text1"]/a[@id="assign"]

Answer №2

If you're looking for a different approach, you could search for the 'tr' element that contains 'td' with the text 'Text1', and then within that 'tr' find the 'td' element with the text 'Assign'.

//table[@id='table']//tr[td[.='Text1']]/td[.='Assign']

When it comes to CSS selectors, there isn't a direct way to search based on text content. The 'contains' selector isn't universally supported yet, so it may not be effective in your particular case.

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

Beside the element, a dropdown navigation menu emerges

Trying to work on a dropdown menu here. It appears to be functioning, but there's a small issue - the dropdown shows up on the right side of the element instead of directly below it. I have a feeling it has to do with either position or padding, but I ...

Warning from Cytoscape.js: "The use of `label` for setting the width of a node is no longer supported. Please update your style settings for the node width." This message appears when attempting to create

I'm currently utilizing Cytoscape.js for rendering a dagre layout graph. When it comes to styling the node, I am using the property width: label in the code snippet below: const cy = cytoscape({ container: document.getElementById('cyGraph&apo ...

What is the method for aligning an <img> to the right within a Bootstrap 4 card-body?

I am having difficulty aligning an image to the right within a Bootstrap 4 card-body. I attempted to use the float-right class, but it doesn't seem to be working. Are there any specific considerations for aligning images in a card-body? <div class ...

Side-menu elevates slider

Struggling to keep my slider fixed when the side-menu slides in from the left? I've scoured for solutions without luck. Any expert out there willing to lend a hand? Code Snippet: https://jsfiddle.net/nekgunru/2/ ...

Issues with aligning text in TextBoxes

I'm struggling to center align my text boxes and write labels on either side. I want it to look like this: https://i.stack.imgur.com/6QsNv.png Despite trying different solutions, I can't seem to get the boxes to align in the center. Is there a ...

Selenium browser profile management

I am curious about how the profile of a browser can impact the output of a Selenium script. Here are the various scenarios to consider: When no profile is specified in the Selenium script, which profile does Selenium open? Is it the default browser pr ...

Creating nested tabs using vanilla JavaScript, no need for jQuery

I am currently working on implementing tabs using a JavaScript function. The issue I am facing is that when I try to have tabs within one of the tabs, everything disappears every time I click on one of the inner tabs. This happens because the JavaScript fu ...

Center a DIV class with CSS

Experiencing some challenges with CSS in relation to layout problems. Take a look at this screenshot: In my CSS, I have implemented the following: .image{float:right; padding-top:10px; padding-left:10px; padding-bottom:10px;} .description{font-size:15px; ...

Trouble with clicking the Sign In button on Outlook using Selenium

I'm having difficulty clicking a button in Outlook. Here is the code for the button: <a data-m="{"cN":"SIGNIN", "sN":"P6", "pV":"1"}" href="https://outlook.live.com/owa/?nlp ...

Obtaining the dynamic id with xpath in Selenium WebDriver

I am having trouble retrieving the id of an element that starts with 'u_' and ends with '_5', with varying data in between. Sometimes it appears as 'u_d_5' while other times it is 'u_6_5'. The usual methods like star ...

My grid layout remains unchanged despite tweaking grid-template-columns

I am currently learning HTML and CSS and experimenting with building a website. My goal is to have 4 boxes aligned next to each other, similar to a navigation bar, but with box items. Here is the code for these boxes: HTML: <section class="boxes&q ...

Ways to decrease the saturation of a background image within a div using CSS

I've come across many plugins that can desaturate an image within the tag. However, I haven't been able to find a solution for desaturating a background image that is used for an element (div). Is there a way to desaturate the background image ...

What are the steps to effectively click on a link using Python and Selenium?

Currently, I am iterating through rows containing a link and an assigned index value. Along with using Selenium, I also utilize the Beautiful Soup API to inspect the page's HTML. The main issue arises when I try to click on the link at a specific ind ...

Using jQuery to submit data via POST method without having the page reload

I have a link in the footer that, when clicked, jumps to the header with a # in the address bar. Is there a way to prevent this and stay in the footer? Here is the code snippet: <a href="#" class="clickIn" id="1" attrIn="Like"><span style="color ...

How can I ensure a DIV is shown on every sub-page?

Is there a way to dynamically display a <div id="chatbox"> in all subpages without needing to manually edit each one? For example, adding this code to the main index.html file and having it appear on /products/index.html. ...

Tips for dividing a div into percentages while maintaining a constant width for a div on the left side

I need help with creating a layout that involves 4 boxes. The first box (box1) should have a width of 50px on the left side, while the remaining 3 boxes should fill in the rest of the screen and be resizable. Can someone guide me on achieving this layout? ...

Optimizing Selenium tests for parallel execution while ensuring thread safety

As I explore parallelizing my Selenium tests, I stumbled upon the fact that Selenium's IWebDriver is not thread safe. Although I'm uncertain of the potential risks of running non-thread safe tests, I want to address any possible "bad practices". ...

Enable a function to be triggered when clicking on an entire div

In my code, I have set an eventHandler on the document itself that targets a specific element with an id. This targeted element is a div containing some content. The eventHandler is triggered by a 'click' action on the document. However, I have e ...

What is causing the issue of my oversized images not showing up on GoDaddy?

I recently uploaded my website on GoDaddy and I am facing an issue where some of my small images (sizes: 872 × 546px) are displaying perfectly fine, but the large ones (banners with a size of 2700 × 900px) aren't showing up. Does anyone have ...

Tips for integrating external JavaScript libraries and stylesheets into a widget

I am currently working on developing a custom Javascript widget that requires users to insert specific lines of code into their web pages. This code will then dynamically add an externally hosted javascript file, allowing me to inject HTML content onto the ...