简体   繁体   English

使用 Selenium 过滤跨度或标记文本 XPATH

[英]Filtering span or mark text XPATH with Selenium

Search box is having Span autocomplete text and with mark as child attribute.搜索框具有 Span 自动完成文本并带有标记为子属性。 在此处输入图片说明

在此处输入图片说明

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.好像'Rush Your File'在列表中的位置将来会发生变化,这个脚本就会失败。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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