0

I'm new to this and have following problem: I want to choose one option from following drop down menu: screenshot of the dropdown menu

This drop down menu is out of sight of the page, meaning you first have to scroll down to see it on the page.

Problem: Java doesn't detect it the drop down menu, nor anything else, that is not in sight / where you first have to scroll down to see it. Java scrolls down to it (e.g. the drop down menu), but it very rarely choose my selected option / does anything with it. But other things, e.g. another drop down menu at the beginning of the page (where you don't have to scroll down to see it) can easily be selected by my code.

Here is my java code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumTest {

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();

//click on drop down menu (no scrolling needed) 
driver.findElement(By.xpath("/html/body/main/div[2]/div/div/div[1]/form/div[3]/div/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]")).click();
    
//choose first option of drop down menue (works)
driver.findElement(By.xpath("/html/body/main/div[2]/div/div/div[1]/form/div[3]/div/div[2]/div[1]/div[2]/div[1]/div[2]/div[2]")).click();
    
    
//here comes the part where you have to scroll down, to see the drop down menu (doesnt work)
//up to here, java scrolls down, but doesnt open the drop down menu.
//click on drop down menu:
driver.findElement(By.xpath("/html/body/main/div[2]/div/div/div[1]/form/div[6]/div/div/div[2]/div[1]/div[1]/div[1]/div[2]/img")).click();
    

//choose drop down menu option:
driver.findElement(By.xpath("/html/body/main/div[2]/div/div/div[1]/form/div[6]/div/div/div[2]/div[1]/div[1]/div[2]/div[1]")).click();
    }
}
I have tried it with the following in between the lines as well already:

Thread.sleep(2000);

and also this:

driver.sleep(2000);

But this all didn't work. I have even tried to press multiple times on the dropdown menu at once, like this:

driver.findElement(By.xpath("/html/body/main/div[2]/div/div/div[1]/form/div[6]/div/div/div[2]/div[1]/div[1]/div[1]/div[2]/img")).click();
driver.findElement(By.xpath("/html/body/main/div[2]/div/div/div[1]/form/div[6]/div/div/div[2]/div[1]/div[1]/div[1]/div[2]/img")).click();
driver.findElement(By.xpath("/html/body/main/div[2]/div/div/div[1]/form/div[6]/div/div/div[2]/div[1]/div[1]/div[1]/div[2]/img")).click();

, which made it work just once but then stopped to work as a solution, when trying to repeat it.

Here is the HTML code of the drop down menu that my java code doesn't work with correctly:

<div data-component-part="selectbox" class="selectBox open"><div data-component-attr="display_value" class="value">
</div><div data-component-part="arrow" class="arrow"><img src="/assets/arrow_down-4d27b98b518b1bc139df08447d0167996f103ae454c1d5331da792a136b3519b.svg">
</div><img class="spinner" src="/assets/spinner_dark-45bc141b45449c6788c5660cb5de41d3316413bb3307607a3722ef8bcbd9acb1.svg" style="display: none;">
</div><div data-component-part="options_container" class="optionsContainer" style="display: block; min-width: 186.35px; height: 95.6px;"><div data-component-part="option" class="option" data-option-value="Bank wire">Bank wire</div><div data-component-part="option" class="option" data-option-value="Cash">Cash</div><div data-component-part="option" class="option" data-option-value="Cryptocurrency">Cryptocurrency</div><div data-component-part="option" class="option" data-option-value="Online payment system">Online payment system</div></div>

3
  • You can try to Wait for an element getting focused/visible and do it the Selenium way… I think a FluentWait might help. Check that link…
    – deHaar
    Commented Aug 5, 2022 at 8:27
  • @deHaar doesn't work out but thanks anyway
    – DasDouble
    Commented Aug 5, 2022 at 8:56
  • Can you share a link to that page? We have absolutely nothing to do with screenshot picture you provided
    – Prophet
    Commented Aug 5, 2022 at 9:29

1 Answer 1

0

Generally click method automatically scrolls the element into view if the element is already visible/present within the HTML DOM.


Solution

You need to scrollIntoView() the desired element and then induce WebDriverWait for the elementToBeClickable() you can use the following solution:

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='selectBox open' and @data-component-part='selectbox']//img[starts-with(@src, '/assets/arrow_down')]"))));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='selectBox open' and @data-component-part='selectbox']//img[starts-with(@src, '/assets/arrow_down')]"))).click();
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='optionsContainer']//div[@data-option-value='Bank wire']"))).click();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.