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();
}
}
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>
Wait
for an element getting focused/visible and do it the Selenium way… I think aFluentWait
might help. Check that link…