@@ -220,6 +220,169 @@ fun main() {
220
220
{{< / code-panel >}}
221
221
{{< / code-tab >}}
222
222
223
+ ### Find Elements using relative locators
224
+
225
+ Note: This feature is available in Selenium 4 or later
226
+
227
+ These feature allows you to find Elements in the DOM based off the position
228
+ of another element using relative locations like ` above ` , ` below ` , ` toLeftOf ` ,
229
+ and ` toRightOf ` . The Elements should be returned in proximity order.
230
+
231
+
232
+ {{< code-tab >}}
233
+ {{< code-panel language="java" >}}
234
+ import org.openqa.selenium.By;
235
+ import org.openqa.selenium.WebDriver;
236
+ import org.openqa.selenium.WebElement;
237
+ import org.openqa.selenium.chrome.ChromeDriver;
238
+ import java.util.List;
239
+
240
+ import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;
241
+
242
+ public class findElementsUsingRelativeLocators {
243
+ public static void main(String[ ] args) {
244
+ WebDriver driver = new ChromeDriver();
245
+ try {
246
+ driver.get("https://example.com ");
247
+
248
+ // Get element with tag name 'div'
249
+ WebElement fromElement = driver.findElement(By.tagName("div"));
250
+
251
+ // Get all the elements available with tag name 'p' above
252
+ // WebElement `fromElement`
253
+ List<WebElement> elements = driver.findElements(
254
+ withTagName("p").above(fromElement)
255
+ );
256
+ for (WebElement e : elements) {
257
+ System.out.println(e.getText());
258
+ }
259
+ } finally {
260
+ driver.quit();
261
+ }
262
+ }
263
+ }
264
+ {{< / code-panel >}}
265
+ {{< code-panel language="python" >}}
266
+ from selenium import webdriver
267
+ from selenium.webdriver.common.by import By
268
+ from selenium.webdriver.support.relative_locator import with_tag_name
269
+
270
+ driver = webdriver.Chrome()
271
+ driver.get("https://www.example.com ")
272
+
273
+ # Get element with tag name 'div'
274
+ form_element = driver.find_element(By.TAG_NAME, 'div')
275
+
276
+ # Get all the elements available with tag name 'p' above
277
+ # WebElement ` fromElement ` '
278
+ elements = elements = driver.find_elements(with_tag_name("p").above(form_element))
279
+ for e in elements:
280
+ print(e.text)
281
+ {{< / code-panel >}}
282
+ {{< code-panel language="csharp" >}}
283
+ using OpenQA.Selenium;
284
+ using OpenQA.Selenium.Chrome;
285
+ using System.Collections.Generic;
286
+
287
+ namespace FindElementsUsingRelativeLocators {
288
+ class FindElementsUsingRelativeLocators {
289
+ public static void Main(string[ ] args) {
290
+ IWebDriver driver = new ChromeDriver();
291
+ try {
292
+ driver.Navigate().GoToUrl("https://example.com ");
293
+
294
+ // Get element with tag name 'div'
295
+ IWebElement fromElement = driver.FindElement(By.TagName("div"));
296
+
297
+ // Get all the elements available with tag name 'p' above
298
+ // WebElement `fromElement'
299
+ ReadOnlyCollection<IWebElement> elements = driver.FindElements(
300
+ RelativeBy.WithTagName("p").Above(fromElement)
301
+ );
302
+ foreach(IWebElement e in elements) {
303
+ System.Console.WriteLine(e.Text);
304
+ }
305
+ } finally {
306
+ driver.Quit();
307
+ }
308
+ }
309
+ }
310
+ }
311
+ {{< / code-panel >}}
312
+ {{< code-panel language="ruby" >}}
313
+ require 'selenium-webdriver'
314
+ driver = Selenium::WebDriver.for : chrome
315
+ begin
316
+ # Navigate to URL
317
+ driver.get 'https://www.example.com '
318
+
319
+ # Get element with tag name 'div'
320
+ from_element = driver.find_element(:tag_name,'div')
321
+
322
+ # Get all the elements available with tag name 'p' above
323
+ # WebElement `fromElement`'
324
+ elements = element.find_elements(
325
+ relative: {tag_name: 'p', above: from_element}
326
+ )
327
+
328
+ elements.each { |e|
329
+ puts e.text
330
+ }
331
+ ensure
332
+ driver.quit
333
+ end
334
+ {{< / code-panel >}}
335
+ {{< code-panel language="javascript" >}}
336
+ const {Builder, By} = require('selenium-webdriver');
337
+
338
+ (async function example() {
339
+ let driver = new Builder()
340
+ .forBrowser('chrome')
341
+ .build();
342
+
343
+ await driver.get('https://www.example.com');
344
+
345
+ // Get element with tag name 'div'
346
+ let fromElement = await driver.findElement(By.css("div"));
347
+
348
+ // Get all the elements available with tag name 'p' above
349
+ // WebElement `fromElement'
350
+ let elements = await driver.findElements(
351
+ withTagName('p').above(fromElement)
352
+ );
353
+ for(let e of elements) {
354
+ console.log(await e.getText());
355
+ }
356
+ })();
357
+ {{< / code-panel >}}
358
+ {{< code-panel language="kotlin" >}}
359
+ import org.openqa.selenium.By
360
+ import org.openqa.selenium.chrome.ChromeDriver
361
+
362
+ fun main() {
363
+ val driver = ChromeDriver()
364
+ try {
365
+ driver.get("https://example.com ")
366
+
367
+ // Get element with tag name 'div'
368
+ val fromElement = driver.findElement(By.tagName("div"))
369
+
370
+ // Get all the elements available with tag name 'p' above
371
+ // WebElement `fromElement''
372
+ val elements = driver.findElements(
373
+ withTagName("p").above(fromElement)
374
+ )
375
+ for (e in elements) {
376
+ println(e.text)
377
+ }
378
+ } finally {
379
+ driver.quit()
380
+ }
381
+ }
382
+ {{< / code-panel >}}
383
+ {{< / code-tab >}}
384
+
385
+
223
386
## Find Element From Element
224
387
225
388
It is used to find a child element within the context of parent element.
@@ -546,23 +709,23 @@ It is used to track (or) find DOM element which has the focus in the current bro
546
709
547
710
## Is Element Enabled
548
711
549
- This method is used to check if the connected Element
712
+ This method is used to check if the connected Element
550
713
is enabled or disabled on a webpage.
551
- Returns a boolean value, ** True** if the connected element is
714
+ Returns a boolean value, ** True** if the connected element is
552
715
** enabled** in the current browsing context else returns ** false** .
553
716
554
717
{{< code-tab >}}
555
718
{{< code-panel language="java" >}}
556
- //navigates to url
719
+ //navigates to url
557
720
driver.get("https://www.google.com/ ");
558
-
721
+
559
722
//returns true if element is enabled else returns false
560
723
boolean value = driver.findElement(By.name("btnK")).isEnabled();
561
724
{{< / code-panel >}}
562
725
{{< code-panel language="python" >}}
563
726
# Navigate to url
564
727
driver.get("http://www.google.com ")
565
-
728
+
566
729
# Returns true if element is enabled else returns false
567
730
value = driver.find_element(By.NAME, 'btnK').is_enabled()
568
731
{{< / code-panel >}}
@@ -591,28 +754,28 @@ await driver.get('https://www.google.com');
591
754
let element = await driver.findElement(By.name("btnK")).isEnabled();
592
755
{{< / code-panel >}}
593
756
{{< code-panel language="kotlin" >}}
594
- //navigates to url
757
+ //navigates to url
595
758
driver.get("https://www.google.com/ ")
596
-
759
+
597
760
//returns true if element is enabled else returns false
598
761
val attr = driver.findElement(By.name("btnK")).isEnabled()
599
762
{{< / code-panel >}}
600
763
{{< / code-tab >}}
601
764
602
765
## Is Element Selected
603
766
604
- This method determines if the referenced Element
605
- is _ Selected_ or not. This method is widely used on
767
+ This method determines if the referenced Element
768
+ is _ Selected_ or not. This method is widely used on
606
769
Check boxes, radio buttons, input elements, and option elements.
607
770
608
- Returns a boolean value, ** True** if referenced element is
771
+ Returns a boolean value, ** True** if referenced element is
609
772
** selected** in the current browsing context else returns ** false** .
610
773
611
774
{{< code-tab >}}
612
775
{{< code-panel language="java" >}}
613
- //navigates to url
776
+ //navigates to url
614
777
driver.get("https://the-internet.herokuapp.com/checkboxes ");
615
-
778
+
616
779
//returns true if element is checked else returns false
617
780
boolean value = driver.findElement(By.cssSelector("input[ type='checkbox'] : first-of-type ")).isSelected();
618
781
{{< / code-panel >}}
@@ -626,7 +789,7 @@ value = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']:first-of-ty
626
789
{{< code-panel language="csharp" >}}
627
790
// Navigate to Url
628
791
driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/checkboxes ");
629
-
792
+
630
793
// Returns true if element ins checked else returns false
631
794
bool value = driver.FindElement(By.CssSelector("input[ type='checkbox'] : last-of-type ")).Selected;
632
795
{{< / code-panel >}}
@@ -645,22 +808,22 @@ await driver.get('https://the-internet.herokuapp.com/checkboxes');
645
808
let res = await driver.findElement(By.css("input[ type='checkbox'] : last-of-type ")).isSelected();
646
809
{{< / code-panel >}}
647
810
{{< code-panel language="kotlin" >}}
648
- //navigates to url
811
+ //navigates to url
649
812
driver.get("https://the-internet.herokuapp.com/checkboxes ")
650
-
813
+
651
814
//returns true if element is checked else returns false
652
815
val attr = driver.findElement(By.cssSelector("input[ type='checkbox'] : first-of-type ")).isSelected()
653
816
{{< / code-panel >}}
654
817
{{< / code-tab >}}
655
818
656
819
## Get Element TagName
657
820
658
- It is used to fetch the [ TagName] ( https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name )
821
+ It is used to fetch the [ TagName] ( https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name )
659
822
of the referenced Element which has the focus in the current browsing context.
660
823
661
824
{{< code-tab >}}
662
825
{{< code-panel language="java" >}}
663
- //navigates to url
826
+ //navigates to url
664
827
driver.get("https://www.example.com ");
665
828
666
829
//returns TagName of the element
@@ -676,7 +839,7 @@ attr = driver.find_element(By.CSS_SELECTOR, "h1").tag_name
676
839
{{< code-panel language="csharp" >}}
677
840
// Navigate to Url
678
841
driver.Navigate().GoToUrl("https://www.example.com ");
679
-
842
+
680
843
// Returns TagName of the element
681
844
string attr = driver.FindElement(By.CssSelector("h1")).TagName;
682
845
{{< / code-panel >}}
@@ -695,17 +858,17 @@ await driver.get('https://www.example.com');
695
858
let value = await driver.findElement(By.css('h1')).getTagName();
696
859
{{< / code-panel >}}
697
860
{{< code-panel language="kotlin" >}}
698
- //navigates to url
861
+ //navigates to url
699
862
driver.get("https://www.example.com ")
700
-
863
+
701
864
//returns TagName of the element
702
865
val attr = driver.findElement(By.cssSelector("h1")).getTagName()
703
866
{{< / code-panel >}}
704
867
{{< / code-tab >}}
705
868
706
869
## Get Element Rect
707
870
708
- It is used to fetch the dimensions and coordinates
871
+ It is used to fetch the dimensions and coordinates
709
872
of the referenced element.
710
873
711
874
The fetched data body contain the following details:
@@ -728,7 +891,7 @@ System.out.println(res.getX());
728
891
{{< code-panel language="python" >}}
729
892
# Navigate to url
730
893
driver.get("https://www.example.com ")
731
-
894
+
732
895
# Returns height, width, x and y coordinates referenced element
733
896
res = driver.find_element(By.CSS_SELECTOR, "h1").rect
734
897
{{< / code-panel >}}
@@ -745,7 +908,7 @@ System.Console.WriteLine(res.Size);
745
908
{{< code-panel language="ruby" >}}
746
909
# Navigate to url
747
910
driver.get 'https://www.example.com '
748
-
911
+
749
912
# Returns height, width, x and y coordinates referenced element
750
913
res = driver.find_element(css: "h1").rect
751
914
{{< / code-panel >}}
@@ -770,7 +933,7 @@ println(res.getX())
770
933
771
934
## Get Element CSS Value
772
935
773
- Retrieves the value of specified computed style property
936
+ Retrieves the value of specified computed style property
774
937
of an element in the current browsing context.
775
938
776
939
{{< code-tab >}}
0 commit comments