【问题标题】:I Need To Scroll Inside a Drop Down Box我需要在下拉框内滚动
【发布时间】:2019-04-04 06:35:14
【问题描述】:

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

上面的代码正在运行,但它也在滚动下拉菜单和网页。

请帮我解决这个问题。

【问题讨论】:

  • 只应用overflow: auto 为role="menu 元素或其类

标签: java html selenium selenium-webdriver


【解决方案1】:

您也可以使用 selenium 的动作类进行滚动, 为此,您需要导入它。

import org.openqa.selenium.interactions.Actions;

WebElement menuItem = Driver.findElement(menuItemPath);
Actions action = new Actions(Driver);
action.moveToElement(menuItem);
action.perform();
menuItem.click();

【讨论】:

    【解决方案2】:

    您的代码是正确的,我在您的代码中没有发现任何问题,它应该滚动直到该元素可见。

    不要使用绝对 xpath,最好始终使用 dynamic xpaths

    请尝试以下代码,

    browser.executeScript('window.scrollTo(0,0);').then(function () {
        page.saveButton.click();
    }) 
    

    【讨论】:

    • 你说得对,你能给我一个清晰的观点吗? @swapnil wandhe
    猜你喜欢
    • 2023-02-25
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 2021-12-24
    相关资源
    最近更新 更多