简体   繁体   English

Selenium (java) 元素不在焦点上的问题

[英]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 没有检测到它的下拉菜单,也没有其他任何东西,这是看不到的/您首先必须向下滚动才能看到它。 Java scrolls down to it (eg the drop down menu), but it very rarely choose my selected option / does anything with it. Java 向下滚动到它(例如下拉菜单),但它很少选择我选择的选项/用它做任何事情。 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:这是我的 java 代码:

 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:这是我的 java 代码无法正确使用的下拉菜单的 HTML 代码:

 <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 .通常,如果元素在HTML DOM中已经可见/存在,则单击方法会自动将元素滚动到视图中。


Solution解决方案

You need to scrollIntoView() the desired element and then induce WebDriverWait for the elementToBeClickable() you can use the following solution:您需要scrollIntoView()所需的元素,然后为elementToBeClickable()诱导WebDriverWait您可以使用以下解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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