Reputation: 1
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
Upvotes: 0
Views: 70
Reputation: 1
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.
Upvotes: 0
Reputation: 193108
Irrespective of the linktext ST WF SETUP_AWS_USB_APPLICATION_12345
being in either of the formats:
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')]")
Upvotes: 0
Reputation: 5909
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.
Upvotes: 1