简体   繁体   中英

Filtering span or mark text XPATH with Selenium

Search box is having Span autocomplete text and with mark as child attribute. 在此处输入图片说明

在此处输入图片说明

Followed the below code and manually incremented the counter to get the last item in the list.
As if the position of 'Rush Your File' in the list changes in the future, this script will fail. Please suggest a better way of handling it.

sendKeys(caseInput, "Rush Your File");
List<IWebElement> elementList = new List<IWebElement>();
elementList = DriverManager.driver.FindElements(By.XPath("//ul/li//span[text()='']//mark")).ToList();
int cntt = 0;
foreach (IWebElement element in elementList)
{
    if (element.Text == componentName)
    {
        cntt = cntt + 1;
        if (cntt == elementList.Count - 1)
        {
            JavaScriptEleClick(element);
            break;
        }
    }
} 

If I understand correctly your problem, there are several suggested results in the autocomplete list while you want to use the one that exactly matches the inserted input text. Right?
If so you simply should take the element with text equals to the inserted string, not just containing it.
As following:

string inputText = "Rush Your File";
string elementXpath = String.Format("//ul/li//span[text()='{0}']//mark", inputText); 
sendKeys(caseInput, inputText);
element = DriverManager.driver.FindElement(By.XPath(elementXpath));
JavaScriptEleClick(element);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM