简体   繁体   中英

Selenium (java) problem with elements which are not in focus

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 (eg the drop down menu), but it very rarely choose my selected option / does anything with it. But other things, eg 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>

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM