Selenium Webdriver Java rootWebElement.findElements игнорировать дочерние элементы? - PullRequest
0 голосов
/ 19 февраля 2019

Итак, вот предпосылка, мой пользовательский поисковик предназначен для навигации по всем дочерним элементам WebElement и поиска его дочерних узлов.

ПРИМЕРНЫЙ КОД

WebDriver driver = new ChromeDriver();
driver.get("https://www.bing.com");
WebElement webElement = new WebDriverWait().until(ExpectedConditions.presenceOfElementLocated(By.TagName("HTML"));

webElement = webElement.findElement(By.id("sb_form_q"));
webElement.sendKeys("Gibraltar");
webElement = webElement.findElement(By.id("sb_form_go"));
webElement.click();

webElement = webElement.findElement(By.id("b_results"));

List<WebElement> elementList = webElement.findElements(By.xpath(".//*"));
for (WebElement webElement : elementList) {
  System.out.println(i + ": " + webElement.getTagName() + "-" + webElement.getText());
  i++;
}

Пожалуйстапримечание: выше приведен пример кода только для имитации того, что я пытаюсь сделать (фактическая работа не разглашается, и я не могу опубликовать ее)

Пример вывода:

0: h2-Gibraltar - Wikipedia
1: a-Gibraltar - Wikipedia
2: div-https://en.wikipedia.org/wiki/Gibraltar
3: div-https://en.wikipedia.org/wiki/Gibraltar
4: cite-https://en.wikipedia.org/wiki/Gibraltar
5: strong-en.wikipedia.org
6: strong-Gibraltar
7: a-
8: span-
9: span-
10: span-
11: div-
12: div-
13: div-
14: ul-
15: li-
16: div-
17: a-
18: img-
19: li-
20: div-
21: a-
22: img-
23: li-
24: div-
25: a-
26: img-
27: li-
28: div-
29: a-
30: img-
31: li-
32: div-
33: a-
34: img-
35: div-Overview
Gibraltar (/dʒɪˈbrɔːltə(r)/; Spanish pronunciation: [xiβɾalˈtaɾ]) is a British Overseas Territory located at the southern tip of the Iberian Peninsula. It has an area of 6.7 km (2.6 sq mi) and is bordered to the north by Spain. The landscape is dominated by the Rock of Gibraltar at the foot of which is a densely populated town area, home to over 30,000 people, primarily Gibraltarians. It shares a maritime border with Morocco.
In 1704, Anglo-Dutch forces captured Gibraltar from Spain during the War of the Spanish Succession on behalf of the Habsburg claim to the Spanish throne. The territory was ceded to Great Britainin perpetuit…
Read more


History




































See all sections
36: div-Overview
Gibraltar (/dʒɪˈbrɔːltə(r)/; Spanish pronunciation: [xiβɾalˈtaɾ]) is a British Overseas Territory located at the southern tip of the Iberian Peninsula. It has an area of 6.7 km (2.6 sq mi) and is bordered to the north by Spain. The landscape is dominated by the Rock of Gibraltar at the foot of which is a densely populated town area, home to over 30,000 people, primarily Gibraltarians. It shares a maritime border with Morocco.
In 1704, Anglo-Dutch forces captured Gibraltar from Spain during the War of the Spanish Succession on behalf of the Habsburg claim to the Spanish throne. The territory was ceded to Great Britainin perpetuit…
Read more


History




































See all sections
37: div-Overview
Gibraltar (/dʒɪˈbrɔːltə(r)/; Spanish pronunciation: [xiβɾalˈtaɾ]) is a British Overseas Territory located at the southern tip of the Iberian Peninsula. It has an area of 6.7 km (2.6 sq mi) and is bordered to the north by Spain. The landscape is dominated by the Rock of Gibraltar at the foot of which is a densely populated town area, home to over 30,000 people, primarily Gibraltarians. It shares a maritime border with Morocco.
In 1704, Anglo-Dutch forces captured Gibraltar from Spain during the War of the Spanish Succession on behalf of the Habsburg claim to the Spanish throne. The territory was ceded to Great Britainin perpetuit…
Read more


History




































See all sections
38: div-Overview
Gibraltar (/dʒɪˈbrɔːltə(r)/; Spanish pronunciation: [xiβɾalˈtaɾ]) is a British Overseas Territory located at the southern tip of the Iberian Peninsula. It has an area of 6.7 km (2.6 sq mi) and is bordered to the north by Spain. The landscape is dominated by the Rock of Gibraltar at the foot of which is a densely populated town area, home to over 30,000 people, primarily Gibraltarians. It shares a maritime border with Morocco.
In 1704, Anglo-Dutch forces captured Gibraltar from Spain during the War of the Spanish Succession on behalf of the Habsburg claim to the Spanish throne. The territory was ceded to Great Britainin perpetuit…
Read more
39: div-Overview
Gibraltar (/dʒɪˈbrɔːltə(r)/; Spanish pronunciation: [xiβɾalˈtaɾ]) is a British Overseas Territory located at the southern tip of the Iberian Peninsula. It has an area of 6.7 km (2.6 sq mi) and is bordered to the north by Spain. The landscape is dominated by the Rock of Gibraltar at the foot of which is a densely populated town area, home to over 30,000 people, primarily Gibraltarians. It shares a maritime border with Morocco.
In 1704, Anglo-Dutch forces captured Gibraltar from Spain during the War of the Spanish Succession on behalf of the Habsburg claim to the Spanish throne. The territory was ceded to Great Britainin perpetuit…
Read more
40: div-Overview
41: img-
42: img-
43: div-Overview
44: div-Overview
45: div-Overview
46: div-Gibraltar (/dʒɪˈbrɔːltə(r)/; Spanish pronunciation: [xiβɾalˈtaɾ]) is a British Overseas Territory located at the southern tip of the Iberian Peninsula. It has an area of 6.7 km (2.6 sq mi) and is bordered to the north by Spain. The landscape is dominated by the Rock of Gibraltar at the foot of which is a densely populated town area, home to over 30,000 people, primarily Gibraltarians. It shares a maritime border with Morocco.
In 1704, Anglo-Dutch forces captured Gibraltar from Spain during the War of the Spanish Succession on behalf of the Habsburg claim to the Spanish throne. The territory was ceded to Great Britainin perpetuit…
Read more
47: div-Gibraltar (/dʒɪˈbrɔːltə(r)/; Spanish pronunciation: [xiβɾalˈtaɾ]) is a British Overseas Territory located at the southern tip of the Iberian Peninsula. It has an area of 6.7 km (2.6 sq mi) and is bordered to the north by Spain. The landscape is dominated by the Rock of Gibraltar at the foot of which is a densely populated town area, home to over 30,000 people, primarily Gibraltarians. It shares a maritime border with Morocco.
In 1704, Anglo-Dutch forces captured Gibraltar from Spain during the War of the Spanish Succession on behalf of the Habsburg claim to the Spanish throne. The territory was ceded to Great Britainin perpetuit…
Read more
48: div-Gibraltar (/dʒɪˈbrɔːltə(r)/; Spanish pronunciation: [xiβɾalˈtaɾ]) is a British Overseas Territory located at the southern tip of the Iberian Peninsula. It has an area of 6.7 km (2.6 sq mi) and is bordered to the north by Spain. The landscape is dominated by the Rock of Gibraltar at the foot of which is a densely populated town area, home to over 30,000 people, primarily Gibraltarians. It shares a maritime border with Morocco.
In 1704, Anglo-Dutch forces captured Gibraltar from Spain during the War of the Spanish Succession on behalf of the Habsburg claim to the Spanish throne. The territory was ceded to Great Britainin perpetuit…
Read more
49: p-Gibraltar (/dʒɪˈbrɔːltə(r)/; Spanish pronunciation: [xiβɾalˈtaɾ]) is a British Overseas Territory located at the southern tip of the Iberian Peninsula. It has an area of 6.7 km (2.6 sq mi) and is bordered to the north by Spain. The landscape is dominated by the Rock of Gibraltar at the foot of which is a densely populated town area, home to over 30,000 people, primarily Gibraltarians. It shares a maritime border with Morocco.
In 1704, Anglo-Dutch forces captured Gibraltar from Spain during the War of the Spanish Succession on behalf of the Habsburg claim to the Spanish throne. The territory was ceded to Great Britainin perpetuit…
50: a-/dʒɪˈbrɔːltə(r)/
51: a-[xiβɾalˈtaɾ]
52: a-British Overseas Territory
53: a-Iberian Peninsula
54: a-bordered to the north
55: a-Rock of Gibraltar
56: a-Gibraltarians
57: a-Morocco
58: br-
59: a-captured Gibraltar
60: a-Spain
61: a-War of the Spanish Succession
62: a-Habsburg
63: a-Great Britain
64: span-in perpetuit…
65: div-Read more
66: a-Read more
67: img-
68: div-
69: p-
70: a-
71: a-
72: a-
73: a-
74: a-
75: a-
76: a-
77: a-
78: br-
79: a-
80: a-
81: a-
82: a-
83: a-
84: a-
85: a-
86: a-
87: a-
88: br-
89: a-
90: a-
91: a-
92: a-
93: div-History
94: div-History
95: div-History
96: img-
97: img-
98: div-History
99: div-History
100: div-History
101: div-
102: div-
103: div-
104: p-
105: span-
106: br-
107: a-
108: a-
109: a-
110: br-
111: a-
112: span-
113: div-
114: a-
115: img-
116: div-
117: p-
118: span-
119: br-
120: a-
121: a-
122: a-
123: br-
124: a-
125: a-
126: a-
127: a-
128: br-
129: a-
130: a-
131: a-
132: a-
133: a-
134: a-
135: a-
136: a-
137: a-
138: a-
139: a-
140: a-
141: a-
142: br-
143: span-
144: br-
145: br-
146: a-
147: div-
148: div-
149: div-
150: div-
151: img-
152: img-
153: div-
154: div-
155: div-
156: div-
157: div-
158: div-
159: p-
160: a-
161: a-
162: a-
163: a-
164: a-
165: a-
166: a-
167: a-
168: span-
169: div-
170: a-
171: img-
172: div-
173: p-
174: a-
175: a-
176: a-
177: a-
178: a-
179: a-
180: a-
181: a-
182: a-
183: br-
184: a-
185: a-
186: a-
187: a-
188: a-
189: a-
190: a-
191: a-
192: br-
193: a-
194: a-
195: a-
196: a-
197: a-
198: a-
199: a-
200: a-
201: a-
202: br-
203: br-
204: a-
205: div-
206: div-
207: div-
208: div-
209: img-
210: img-
211: div-
212: div-
213: div-
214: div-
215: div-
216: div-
217: p-
218: a-
219: a-
220: a-
221: a-
222: a-
223: a-
224: a-
225: a-
226: span-
227: div-
228: a-
229: img-
230: div-
231: p-
232: a-
233: a-
234: a-
235: a-
236: a-
237: a-
238: a-
239: a-
240: a-
241: br-
242: a-
243: a-
244: a-
245: a-
246: a-
247: a-
248: br-
249: a-
250: a-
251: a-
252: br-
253: span-
254: br-
255: a-
256: a-
257: a-
258: a-
259: br-
260: br-
261: a-
262: div-
263: div-
264: div-
265: div-
266: img-
267: img-
268: div-
269: div-
270: div-
271: div-
272: div-
273: div-
274: p-
275: a-
276: a-
277: a-
278: a-
279: a-
280: a-
281: span-
282: div-
283: a-
284: img-
285: div-
286: p-
287: a-
288: a-
289: a-
290: a-
291: a-
292: a-
293: br-
294: a-
295: a-
296: br-
297: a-
298: a-
299: a-
300: a-
301: a-
302: a-
303: a-
304: br-
305: a-
306: a-
307: a-
308: br-
309: br-
310: a-
311: div-
312: div-
313: div-
314: div-
315: img-
316: img-
317: div-
318: div-
319: div-
320: div-
321: div-
322: div-
323: p-
324: a-
325: br-
326: span-
327: br-
328: a-
329: a-
330: span-
331: div-
332: a-
333: img-
334: div-
335: p-
336: a-
337: br-
338: span-
339: br-
340: a-
341: a-
342: br-
343: a-
344: a-
345: a-
346: a-
347: a-
348: a-
349: a-
350: a-
351: a-
352: a-
353: br-
354: br-
355: span-
356: br-
357: a-
358: a-
359: a-
360: a-
361: a-
362: a-
363: a-
364: a-
365: a-
366: br-
367: a-
368: a-
369: a-
370: a-
371: a-
372: br-
373: br-
374: a-
375: div-
376: div-
377: div-
378: div-
379: img-
380: img-
381: div-
382: div-
383: div-
384: div-
385: div-
386: div-
387: p-
388: a-
389: a-
390: a-
391: a-
392: a-
393: a-
394: a-
395: a-
396: a-
397: br-
398: span-
399: div-
400: a-
401: img-
402: div-
403: p-
404: a-
405: a-
406: a-
407: a-
408: a-
409: a-
410: a-
411: a-
412: a-
413: br-
414: a-
415: a-
416: a-
417: a-
418: div-
419: div-
420: div-
421: img-
422: img-
423: div-
424: div-
425: div-
426: div-
427: div-
428: p-
429: a-
430: div-See all sections
431: a-See all sections
432: div-See all sections
433: div-See all sections
434: div-
435: div-
436: div-
437: a-
438: span-
439: a-

Я обнаружил, что этот процесс также добавляет всех дочерних элементов в указанный список.

Как игнорировать указанные дочерние элементы в списке?

(Извините, если об этом уже спрашивали, но я не могу его найти.)

Ответы [ 2 ]

0 голосов
/ 19 февраля 2019

Ваш xpath неверен .//*.

<father>
    <child1>
        <child2></child2>
    </child1>
    <child1>
        <child2></child2>
    </child1>
</father>

В этом примере вы получите все от father - child1 и child2

Чтобы получить только дочерние элементы webelement без дочерних элементов (childs of childs), вынужно использовать ./

С ./ вы получите только child1

0 голосов
/ 19 февраля 2019
List<WebElement> elementList = webElement.findElements(By.xpath(".//*"));

против

List<WebElement> elementList = webElement.findElements(By.xpath("//*"));

"."в вашем xpath хватает всех детей.

KunLun прав.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...