简体   繁体   中英

Sorting a WebElement List to find specific Element (Selenium WebDriver/Java)

I'd like to search for a very specific WebElement in a very specific way.

The WebElement looks like this:

<aw>
    <div class="class">
        <h1>
            <a class="class1">TEXT TO FIND 1</a> 
        </h1>
        <p>
            <a class="class2"> TEXT TO FIND 2 </a>
        </p>
    </div>
</aw>

Both the "text to find" , may be written like this "te xt to fi nd" (this makes obsolete contains()). Because of this, I thought of subdividing both strings that I need to search in characters and then search for an element who contains all the characters, with max a 1 char tolerance.

Example of this^

getCorrectElement(String1, String2) -> returns WebElement

Searching for "text" in (h1/a) [String1] and for "cat" in (p/a) [String2]

  • Split both strings into array of characters

  • Make a list of all elements who contain "t" ;

  • Get all the entries of that list containing "e" ;

  • Repeat until you have no words left/no element found,

    if no element is found, try to see if there is a previous one (or maybe a combination of the characters (as always, we have 1 character tolerance), ex. 1 2 3 not found, but we have an element who matches 2 and 3 , if we have something like this, select this element (or better yet, add it to the new list).

  • Now we have a list that contains only WebElements which have "text" as (h1/a) we will use this to refine our search.

  • We use the list we just made and do the exact same process as before.

  • Operation finished (found element with both "text"(h1/a) and "cat"(p/a), return correct WebElement

This means that if "Cool Catto" was inputted, but there was no element matching it completely, it goes on the next element who matches most of it (max 1 char tolerance).

So "Cool Catto" = "Col Catt" , also "Cool Catto" = "C ool Catt o"

I tried getting the xpath search toText(), but this xpath expression a[contains(text(), ' text to find ')]/../../p/a[contains(text(),' text to find')] , returns only "text to find 2" and not both.

If I use [contains() and contains()] , the expression returns elements who might only match a single "text to find" , while I need something that only matches both.

Also, I am pretty sure you can't do String->WebElement, So this method is always been wrong

How would I do something like this? Are there any more ways? I thought of maps, but I haven't much experience with those.

The performance will be much worse, but you can return a list of possible elements and then perform more advanced filtering in your Java code. I'm using C# in my example, but the Java equivalent should be very similar.

var element = webDriver
    // Find all elements that are a direct child of the parent element
    .FindElements(By.CssLocator("aw > div.class > *"))
    // Get the first element matching the criteria
    .FirstOrDefault(e => {
        // Simply remove spaces. Regex could also be used
        string.Equals(e.Text.Replace(" ", ""), "TEXTTOFIND");
    });

I stated the performance will be worse because the WebDriver will have to query/serialize more objects, which if many can cause a noticeable delay. This is the pattern I've used only when a CSS or jquery selector was insufficient. [We] have hundreds of tests that take a few hours to run, so these delays were a considerable issue.

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