2017-02-20 17 views
0

我已經瀏覽了之前的文章,並且仍然遇到問題,無法正常工作。在Java/Selenium中水平滾動的問題

我有一個小的滾動條,我需要向右移動以便可以訪問需要測試的項目。滾動條的代碼如下:

<div class="ngscroll-scrollbar" ng-style="styles.scrollbar" style="height: 
6px; bottom: 3px; left: 3px; opacity: 0; width: 126.05px; transition: 
opacity 0.3s ease-in-out 0s, border-radius 0.1s linear 0s, height 0.1s 
linear 0s, bottom 0.1s linear 0s; position: absolute; cursor: default; 
background: rgba(0, 0, 0, 0.6) none repeat scroll 0% 0%; border-radius: 3px;"></div> 

滾動條是在一個容器內,我不知道這是一個問題。

<div class="ngscroll-scrollbar-container" ng-show="!isTouch" ng- 
style="styles.scrollbarContainer" style="bottom: 0px; left: 0px; width: 
100%; height: 12px; margin-left: 0px; position: absolute; transition: 
background 0.3s ease-in-out 0s; border-radius: 6px; background: transparent 
none repeat scroll 0% 0%;"> 

這裏是我的代碼:

WebElement scroll = driver.findElement(By.xpath("//html/body/div[2]/main/div/ui-view/ui-view/div[2]/div/div[1]/su-flight-filters/div/aside/div/section/div/div/div[2]/div/div[2]/div")); 
JavascriptExecutor js = (JavascriptExecutor)driver; 
js.executeScript("document.getElementsByClassName('ngscroll-scrollbar').scrollRight += 50", ""); 

我沒有得到任何錯誤,但滾動條沒有任何移動。我試過了50,250和400的值。

任何人有什麼想法?

回答

0

.getElementsByClassName()返回一個集合。嘗試

js.executeScript("document.getElementsByClassName('ngscroll-scrollbar')[0].scrollRight += 50", ""); 

滾動第一個匹配元素。你必須檢查並確保第一場比賽是你想要的。

+0

這似乎並不奏效。我不知道如何處理該集合,有什麼方法可以將內容輸出到控制檯以查看它包含的內容嗎? – Sulteric

+0

您可以在控制檯中運行'document.getElementsByClassName('ngscroll-scrollbar').length'並查看該定位器有多少匹配。你應該能夠在控制檯中運行這個JS,看看它是否有效。 – JeffC

0

使用Action類移動滑塊時,我覺得這是作爲convnent方式 選擇X的值是任何取決於你的滑塊的寬度,如果你使用的循環在多個位置拖動指針

WebElement slider = driver.findElement(By.id("slider")); 
    int width=slider.getSize().getWidth(); 
    Actions move = new Actions(driver); 
    Action action = (Action) move.dragAndDropBy(slider, 30, 0).build(); 
    action.perform(); 
+0

我認爲問題是這個特定的滾動條必須點擊然後拖動。以上是否會這樣做?我試了一下,我發現它似乎被激活,但不會滑動。酒吧是205寬,6高。 – Sulteric

+0

或通過指定範圍in for循環使用此代碼 move.moveToElement(滑塊).click(滑塊).sendKeys(Keys.ARROW_RIGHT).perform(); – Dharam

+0

或者在動作對象中使用下面的這一個,指定要拖動的像素數 move.moveToElement(draggablePartOfScrollbar).clickAndHold()。moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release()。perform(); – Dharam