Home > Software design >  Spring Boot Selenium - Github Dropdown Cannot Work. How can I fix it?
Spring Boot Selenium - Github Dropdown Cannot Work. How can I fix it?

Time:12-04

I tried to implement a logout test method through selenium in Spring Boot but I cannot detect dropdown menu located top right hand side.

How can I fix it?

Here is the test method shown below.

@Test
@Order(4)
public void logout() throws InterruptedException {

    login();

    driver.get("https://github.com");
    Thread.sleep(1000);

    // Header-item position-relative mr-0 d-none d-md-flex
    WebElement profileDropdown = driver.findElement(By.cssSelector(".Header-item.position-relative.mr-0.d-none.d-md-flex")); // cannot work

    // dropdown-item dropdown-signout
    WebElement signOutButton = driver.findElement(By.cssSelector(".dropdown-item.dropdown-signout")); // cannot work

    profileDropdown.click();
    Thread.sleep(1000);

    signOutButton.click();
}

Here is the error part shown below

java.net.SocketException: Connection reset
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".dropdown-item.dropdown-signout"}

1st Edited

String xpathProfile = "//*[@aria-label='View profile and more']";
WebElement profileDropdown = driver.findElement(By.xpath(xpathProfile));
    
String xpathSignOut = "//button[contains(@class,'dropdown-signout')]";
WebElement signOutButton = driver.findElement(By.xpath(xpathSignOut));

I got this issue shown below.

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(@class,'dropdown-signout')]"}

CodePudding user response:

This is a bad practice to try to locate elements like this, you should be more specific. Given this DOM that you are working with I would try using a selector somewhat like this:

String xpathProfile = "//*[@aria-label='View profile and more']"; 
String xpathSignOut = "//button[contains(@class,'dropdown-signout')]";

As you can see it's an xpath type selector and I would recommend learning xpath as it is far more readable once you get used to it and it also works in a few edge cases where you wouldn't be able to use css selector.

Also I've noticed that if you make the window size smaller, at some point the profile button you are trying to click is hidden in github, so maybe that is why your button is not getting clicked. You could try setting a specific bigger window size using chromeOptions:

ChromeOptions options = new ChromeOptions();
options.addArgument("--window-size=1920,1080");
ChromeDriver driver = new ChromeDriver(options);

Can you actually see the button when the test is running?

CodePudding user response:

Here is the answer shown below

public void logout() throws InterruptedException {

        login();

        driver.get("https://github.com");
        Thread.sleep(1000);

        // Header-item position-relative mr-0 d-none d-md-flex
        WebElement profileDropdown = driver.findElement(By.cssSelector(".Header-item.position-relative.mr-0.d-none.d-md-flex"));
        profileDropdown.click();

        Thread.sleep(1000);

        // dropdown-item dropdown-signout
        WebElement signOutButton = driver.findElement(By.cssSelector(".dropdown-item.dropdown-signout"));
        signOutButton.click();

        Thread.sleep(2000);
        
    }
  • Related