在Selenium中,我正在自动化,但是在该下拉菜单中,我需要单击“隐藏的元素”。
它向下滚动整个页面,我只希望特定下拉菜单向下滚动而不是页面。
完整Div的HTML代码:
<ul class="dropdown-menu drop_Loc" role="menu" style="display: none;" xpath="1"> <li>
<a href="carle_place.aspx" target="_self">
<span class="icon-fort icon-left" style="font-size: 40px;padding: 0px 8px;"></span> <span class="nav_icon_heading">Carle Place</span>
</a>
</li>
<li>
<a href="Selling-a-car-cutler-bay.aspx" target="_self">
<span class="icon2-cutlerbay icon-left" style="font-size: 24px;padding: 7px 8px;"></span>
<span class="nav_icon_heading">Cutler Bay</span>
</a>
</li>
<li>
<a href="Daytona.aspx" target="_self">
<span class="icon-daytona icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Daytona</span>
</a>
</li>
<li>
<a href="Fort_lauderdale.aspx" target="_self">
<span class="icon-fort icon-left" style="font-size: 40px;padding: 0 8px"></span> <span class="nav_icon_heading">Fort Lauderdale</span>
</a>
</li>
<li>
<a href="Sell-my-car-longwood.aspx" target="_self">
<span class="icon-orlando icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Longwood</span>
</a>
</li>
<li>
<a href="Melbourne.aspx" target="_self">
<span class="icon-melbourne icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Melbourne</span>
</a>
</li>
<li>
<a href="miami.aspx" target="_self">
<span class="icon-miami icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Miami</span>
</a>
</li>
<li>
<a href="Orlando.aspx" target="_self">
<span class="icon-orlando icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Orlando</span>
</a>
</li>
<li>
<a href="St_James.aspx" target="_self">
<span class="icon-daytona icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">St James</span>
</a>
</li>
<li>
<a href="Treasure_coast.aspx" target="_self">
<span class="icon-treasure_coast icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Stuart</span>
</a>
</li>
<li>
<a href="Tampa.aspx" target="_self">
<span class="icon-treasure_coast icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Tampa</span>
</a>
</li>
<li>
<a id="last-child-location" href="West_palm_beach.aspx" target="_self">
<span class="icon-palm_beach icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">West Palm Beach</span>
</a>
</li>
</ul>
我尝试过这些代码,
By Stuart = By.xpath("/html[1]/body[1]/div[1]/nav[1]/div[1]/div[2]/ul[1]/li[2]/ul[1]/li[10]/a[1]/span[2]")
;
JavascriptExecutor je = (JavascriptExecutor) driver;
WebElement element = driver.findElement(Stuart);
je.executeScript("arguments[0].scrollIntoView(true);",element);
Thread.sleep(4000);
上面的代码可以正常工作,但同时可以滚动下拉列表和网页。
请帮我把这些人分类。
答案 0 :(得分:1)
您的代码正确,我没有在您的代码中发现任何问题,它应该滚动到该元素可见为止。
不要使用绝对xpath,最好始终使用dynamic xpaths。
请尝试以下代码,
browser.executeScript('window.scrollTo(0,0);').then(function () {
page.saveButton.click();
})
答案 1 :(得分:0)
您还可以使用硒的动作类进行滚动, 为此,您将需要导入它。
import org.openqa.selenium.interactions.Actions;
WebElement menuItem = Driver.findElement(menuItemPath);
Actions action = new Actions(Driver);
action.moveToElement(menuItem);
action.perform();
menuItem.click();