首页 > 解决方案 > 无法通过将 selenium 与 Java 一起使用来单击动态更改的 div

问题描述

当我输入 www.reddit.com 并将查询粘贴到搜索字段时,发送回车并转到带有 subreddit 的第一个有效链接,我确实有排序选项 - 默认情况下它设置为 BEST,但我想更改它到顶部。我正在使用带有 BDD 和 POP 的 Java。这是我的测试类代码: public void top_most_top_tile_will_be_printed_on_screen() throws Throwable { redditDetailsPage.changeSorting(); }

我的页面代码:

@FindBy (id = "CommentSort--SortPicker")
private WebElement sortingOption;
@FindBy (xpath = "/html/body/div[3]/a[2]/button")
private WebElement topOption;

public RedditDetailsPage(WebDriver driver) {
    super(driver);
}

public RedditDetailsPage changeSorting(){
    sortingOption.click();
    topOption.click();
    return this;
}

我相信问题出在 topOption xpath 上。我试图通过父 div 类或父类名来定位它,但我总是得到:

org.openqa.selenium.NoSuchElementException: Unable to locate element: /html/body/div[3]/a[2]/button

我正在使用 ExpectedConitions,但看起来我的代码无法看到它的存在 - 一旦我单击sortingOption.click(),这个 div 就会被注入: 点击后出现的 div

我还想补充一点,如果我手动执行此操作,则此 xpath 有效,并且元素被突出显示。我不知道我还能做些什么来对这个列表进行排序。

您知道如何单击排序选项并使用 Selenium 从列表中选择 TOP 吗?


我不知道这怎么可能,但我比较了从手动测试中收集的 div,并且这个元素的 xpath 是: /html/body/div[3]/a[2]/button 但是当我从调试中做了同样的事情时(当打开 selenium 的浏览器时),它指向: /html/body/div[4]/a[2]/button

所以实际上问题出在xpath,但是TBH,我不知道为什么。有人可以在他们的机器上试试吗?

标签: javahtmlseleniumdynamic

解决方案


根据我的建议,您可以使用Chropath,它是 Chrome 浏览器的扩展。Chropath 会尝试找到合适的 xpath 然后尝试使用它

查看更多详细信息并在以下位置下载:https ://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo?hl=en

如果您无法执行单击,请尝试使用 JS,如下所示。

您可以使用 JS 来执行点击动作。[这里是 C# 的方法,但它与 Java 几乎相同]。

 public static void scrollElementToClick(IWebDriver driver, IWebElement element)
{
    IJavaScriptExecutor ex = (IJavaScriptExecutor)driver;
    ex.ExecuteScript("arguments[0].click();", element);
}

元素无法点击的另一件事可能来自页面中表示的元素,但尚未准备好点击[可点击]。因此,您可以尝试在单击操作之前使用提供的等待来等待元素

如果您不能这样做,请尝试使用它来验证您何时要对此元素执行任何操作。(这是用于 C# 的代码,但我认为您可能有这个想法):

 public static bool existsElement(IWebDriver _driver,By by,int waitBySecond)
    {
        WebDriverWait wait = new WebDriverWait(_driver, new TimeSpan(0, 0,waitBySecond));



        try
        {

          // wait[wait.until] for element or search element [driver.FindElement]            
        }
        catch (WebDriverTimeoutException e)
        {
            // Timeout that set for finding element        
            return false;
        }
        catch(NoSuchElementException e)
         {   
           // there is no element in this page
           return false;
         }
        catch(Exception e)
       {  return false;
       }
        return true;
    }

问:您知道如何使用 Selenium 单击排序选项并从列表中选择 TOP 吗?

A:这里有一些选项,您可以使用它来申请

         SelectElement changeOwnerMethodSelectedUser = new 
         SelectElement(_driver.FindElement(By.Name("selectedUser")));
                       // then select one choice from options by text appearance
                        changeOwnerMethodSelectedUser.SelectByText("Choice1");
                      // or you might choose by index like this [ Recommended - If you want to select choice by Top option]
                          changeOwnerMethodSelectedUser.SelectByIndex(3);

注意:所有这些源代码都用于 C#。请在测试之前将语法更改为 Java。


推荐阅读