SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.26k stars 8.15k forks source link

[🐛 Bug]: Relative Locator is not working as expected in this specific case #13642

Open MohabMohie opened 6 months ago

MohabMohie commented 6 months ago

What happened?

While teaching a class about test automation to the students of Saudi Digital Academy we wrote this code:

public class RelativeLocatorsTabularTests {
    WebDriver driver;

    @Test
    public void checkContactNameForTargetCompany(){
        // TODO: Report Bug to Selenium Team
        By contactNameForTargetCompany =
                RelativeLocator.with(By.tagName("td"))
                        .toRightOf(By.xpath("//td[text()='Alfreds Futterkiste']"))
                        .below(By.xpath("//th[text()='Contact']"));

        var actualName = driver.findElement(contactNameForTargetCompany).getText();
        Assertions.assertEquals("Maria Anders", actualName);
    }

    @BeforeEach
    public void beforeEach(){
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.navigate().to("https://www.w3schools.com/html/html_tables.asp");
    }
    @AfterEach
    public void afterEach(){
        driver.quit();
    }
}

We expected to locate the name of the Contact but the actual behavior was that it located the name of the Country (Germany).

This is the website we used to test: https://www.w3schools.com/html/html_tables.asp Here's a screenshot of the target element and page dom:

image

How can we reproduce the issue?

You can use the same code snippet and it will work as-is

Relevant log output

N/A - we debugged it and found that selenium read the value Germany, but we didn't dive deeper

Operating System

Windows 11

Selenium version

Java 4.18.1

What are the browser(s) and version(s) where you see this issue?

Chrome 122.0.6261.70 (Official Build) (64-bit)

What are the browser driver(s) and version(s) where you see this issue?

Selenium Manager is downloading it automatically

Are you using Selenium Grid?

No

github-actions[bot] commented 6 months ago

@MohabMohie, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

joerg1985 commented 2 months ago

I had a look at this, as i had another issue with relative locators.

Without debugging i think this is related due to the definition of below and rightOf, both compare the value with <.

e.g. element.left + element.width < other.left, but other.left is in this case identicall to element.left + element.width (the table uses border collaps).

I am kind of surprised about this definitions, they are not very intuitive. e.g. right of the < Previouse button i would expect the orange area, but it is yellow one.

image

Is it realistic to change this e.g. with selenium 5? The implementation should be not to hard. e.g. for rightOf: Take the rect of the element it should be relative to, move it by element.width to the right and set the rect.width to "max int", as soon as the element rectangle of the element to check intersects this rectangle it is a hit.

PS: the actual issue i hit is a case when the locators match multiple elements. e.g.

RelativeLocator.with(By.tagName("a")) .below(By.tagName("div"));

Will take the first By.tagName("div") and return elements relative to it, not for all divs on the page.

diemol commented 2 months ago

I don't think many people use relative locators. Feel free to make a change and add a good commit message so it is shown in the release. No need to wait for Selenium 5.

bischoffdev commented 2 weeks ago

We do use relative locators and came across this today. It seems to fail if the x position of the element to the right is the same as the x position of the left element plus its width.