0

I Have a link of an account in my web page. Based on the length of the link, it will either display as single line link or multi line link. How to identify the link when its displayed in multiple lines using selenium.

ex: 'ST WF SETUP_AWS_USB_APPLICATION_12345' this link name is displayed as a single line link

If i change the name of account to 'ST WF SETUP_AWS_USB_APPLICATION_SETUP_AWS_USB_123456', link displayed as a 2 line link. like below -

'ST WF SETUP_AWS_USB_APPLICATION_ SETUP_AWS_USB_123456'

As the link is divided into 2 lines, i can see
tag in link name and not able to identify using "By.linkname"

I want to have a unique code to identify the link irrespective of number of lines its divided into using Selenium

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Divya A
  • 1
  • 1
  • 1
    Have you Google for it first? https://stackoverflow.com/questions/29796984/java-selenium-how-to-get-linktext-anchor-from-link-webelement – Shiham Oct 01 '19 at 14:39
  • 1
    Possible duplicate of [Java Selenium, how to get linkText (anchor) from link WebElement](https://stackoverflow.com/questions/29796984/java-selenium-how-to-get-linktext-anchor-from-link-webelement) – Meraj al Maksud Oct 01 '19 at 14:48
  • 1
    @MerajalMaksud Not a duplicate. The link you posted is getting link text for a single element. This user is asking a question about multiple elements, and the strategy is much different. – Christine Oct 01 '19 at 14:52
  • Okay, my bad. I should've noticed. Retracting now. – Meraj al Maksud Oct 01 '19 at 14:58
  • 1
    @Divya A if you post some more HTML for the elements surrounding the link, we can help troubleshoot your problem better. – Christine Oct 01 '19 at 14:59
  • Are you sure the link actually takes up two lines, or is just displayed over two lines? You will need to post the relevant HTML! – SiKing Oct 01 '19 at 20:32

3 Answers3

1

You may need to write a wrapped selector for this which evaluates the link state and determines which selector to use. You will need to implement a quick way to detect how many lines the link takes up.

For multiple link elements, you will not be able to find directly through the name, since it is split up and we have no way of determining which text is split where. But we can write an XPath to the link elements that can find them without using the link text.

If you post some more HTML for the page you are using, I can write a better XPath to help get the links.

public static bool ElementExists(this IWebDriver driver, By by)
{
    return driver.FindElements(by).Count > 0;
}

// get 1 or multi-line link elements
public IWebElement GetLinkElements(string accountName)
{
    // case: single-line link
    if (driver.ElementExists(By.XPath("$//a[text()='{accountName}']")))
    {
        return driver.FindElement(By.XPath("$//a[text()='{accountName}']"));
    }

    // case: multi-line link, use FindElements for multiple
    else {
        return driver.FindElementsBy.XPath("//a"); // this should be more specific
    }
}

This is a generic example, but if you include some HTML for what the rest of the page around the links looks like, we can try to write a better XPath to find them.

Christine
  • 5,567
  • 2
  • 12
  • 35
0

Irrespective of the linktext ST WF SETUP_AWS_USB_APPLICATION_12345 being in either of the formats:

  • single line
  • two lines

You can use either of the following Locator Strategies:

  • partialLinkText:

    By.partialLinkText("SETUP_AWS_USB_APPLICATION_SETUP_AWS_USB_123456")
    
  • xpath:

    By.xpath("//a[contains(.,'SETUP_AWS_USB_APPLICATION_SETUP_AWS_USB_123456')]")
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0

thank you for the response. I tried both the ways of using Xpath & xpath contains but as the source code contains
tag, it dint work.

So am taking subset of my string & verifying using partial link text & it worked for multiple set of data.

Divya A
  • 1
  • 1