[英]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. 我想以一种非常特定的方式搜索一个非常特定的WebElement。
The WebElement looks like this: WebElement看起来像这样:
<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()). 这两个
"text to find"
可以这样写: "te xt to fi nd"
(这使过时的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. 因此,我想到了将需要搜索的两个字符串细分为字符,然后搜索包含所有字符的元素,最大容差为1个字符。
Example of this^ 这个例子^
getCorrectElement(String1, String2) -> returns WebElement getCorrectElement(String1,String2)->返回WebElement
Searching for "text" in (h1/a) [String1] and for "cat" in (p/a) [String2]
在(h1 / a)[String1]中搜索“文本”,在(p / a)[String2]中搜索“ cat”
Split both strings into array of characters 将两个字符串都分成字符数组
Make a list of all elements who contain "t"
; 列出所有包含
"t"
元素;
Get all the entries of that list containing "e"
; 获取该列表中包含
"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). if
未找到任何元素,请尝试查看是否有前一个(或字符的组合(一如既往,我们具有1个字符的公差),例如 1
2
3
找不到,但是我们有一个匹配 2
的元素 和 3
, 如果我们有类似的内容,请选择该元素(或者更好的是,将其添加到新列表中)。
Now we have a list that contains only WebElements which have "text" as (h1/a) we will use this to refine our search. 现在,我们有了一个列表,其中仅包含Web元素,这些Web元素的“文本”为(h1 / a),我们将使用它来优化搜索。
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 操作完成(找到同时带有“文本”(h1 / a)和“猫”(p / a)的元素,返回正确的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). 这意味着,如果输入了
"Cool Catto"
,但没有完全匹配的元素,它将继续与大部分元素匹配的下一个元素(最大1个字符的容差)。
So "Cool Catto"
= "Col Catt"
, also "Cool Catto"
= "C ool Catt o"
因此,
"Cool Catto"
= "Col Catt"
,也"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. 我尝试让xpath搜索到toText(),但是此xpath表达式
a[contains(text(), ' text to find ')]/../../p/a[contains(text(),' text to find')]
,仅返回"text to find 2"
,而不同时返回两者。
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. 如果我使用
[contains() and contains()]
,则表达式返回的元素可能只匹配单个"text to find"
,而我需要的东西只能匹配两个。
Also, I am pretty sure you can't do String->WebElement, So this method is always been wrong 另外,我很确定你不能做String-> WebElement,所以这个方法总是错误的
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. 性能会差很多,但是您可以返回可能的元素列表,然后在Java代码中执行更高级的过滤。 I'm using C# in my example, but the Java equivalent should be very similar.
我在示例中使用C#,但是等效的Java应该非常相似。
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. 我说过性能会变差,因为WebDriver将不得不查询/序列化更多对象,如果对象过多,则可能导致明显的延迟。 This is the pattern I've used only when a CSS or jquery selector was insufficient.
这是我仅在CSS或jquery选择器不足时使用的模式。 [We] have hundreds of tests that take a few hours to run, so these delays were a considerable issue.
[我们有数百个测试,这些测试需要几个小时才能运行,所以这些延迟是一个相当大的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.