0

I want to write a reusable function/method which I can use to check whether a given WebElement is before/after another WebElement

The signature should look like follows:

public boolean isAfter(WebElement first, WebElement second)//returns true if the second is after first else false

Does the Selenium have an API that can allow achieving the scenario? Note that my teammates would be using this function in their respective projects. So my function wouldn't know how HTML in actual looks like to take any other path.

  • When you write "after" do you mean "after" in a x-y coordinate sense or do you mean 'after' in terms of the DOM ? – Michael Durrant Apr 13 at 10:38
  • 1
    What do mean by "before/after"? @PDHide, e.g., assumed you are talking about rendered screen location. On the other hand, DOM is a tree-like structure, and you may consider as "before/after" as "higher lower level" or "parent/child". – João Farias Apr 13 at 10:38
  • Could you please add the use case also , – PDHide Apr 13 at 10:45
1
Point point=driver.findElement(By.id("something")).getLocation();
System.out.println("X Position : "point.x);
System.out.println("Y Position : "point.y);

This will let u find the position of an element in the entire Page . So if a.y>b.y we can say b is after a ( means below a).

Let me know if this is what you meant

| improve this answer | |
  • 1
    depnds on whether "after" means xy or means DOM as we're asking in the question. – Michael Durrant Apr 13 at 10:39
  • @MichaelDurrant true and it would be helpful if the question could mention why to know this in the first place . – PDHide Apr 13 at 10:44
0

If this is about the DOM...

Xpath is your friend and this (relative addressing) is the main case where you choose to use xpath over css (css being often preferred for readability.

The key feature that you can use here is relative addressing

More info at https://www.toolsqa.com/selenium-webdriver/choosing-effective-xpath/

Key info from that link:

Absolute xpath: /html/body/div[2]/div/div/footer/section[3]/div/ul/li[3]/a

Relative xpath: //*[@id=’social-media’]/ul/li[3]/a

Absolute xpath uses a single slash at the start of the xpath and relative uses a double slash.

Using this info you should be able to put together the method you need.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.