簡體   English   中英

Selenium (java) 元素不在焦點上的問題

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

我是新手,遇到以下問題:我想從以下下拉菜單中選擇一個選項:下拉菜單的屏幕截圖

這個下拉菜單不在頁面的視線范圍內,這意味着您首先必須向下滾動才能在頁面上看到它。

問題:Java 沒有檢測到它的下拉菜單,也沒有其他任何東西,這是看不到的/您首先必須向下滾動才能看到它。 Java 向下滾動到它(例如下拉菜單),但它很少選擇我選擇的選項/用它做任何事情。 但我的代碼可以輕松選擇其他內容,例如頁面開頭的另一個下拉菜單(您不必向下滾動即可查看它)。

這是我的 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(); } }
我已經在兩行之間嘗試了以下內容:

 Thread.sleep(2000);

還有這個:

 driver.sleep(2000);

但這一切都沒有奏效。 我什至嘗試在下拉菜單上一次按多次,如下所示:

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

,這使它只工作一次,但在嘗試重復它時停止作為解決方案工作。

這是我的 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>

通常,如果元素在HTML DOM中已經可見/存在,則單擊方法會自動將元素滾動到視圖中。


解決方案

您需要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