【问题标题】:Selenium Java Element Not VisibleSelenium Java 元素不可见
【发布时间】:2018-02-13 12:37:41
【问题描述】:

我在元素的可见性方面遇到了错误。我这样声明我的网络元素:

@FindBy(css=".xyz") private WebElement operatorSource;

我使用以下方法调用这个元素:

@Override
protected WebElement getOperatorSourceWebElement() {
    return operatorSource;
}

我尝试获取带有“.xyz”类的按钮:

<div class="sources">
        <c:forEach var="source" items="${operatorSources}">
            <button type="button" class= "xyz <c:if test="${source eq operatorSource}">active</c:if>" onclick="changeOperatorSource('${source}', this);">${source.description}</button>
        </c:forEach>
    </div>

我尝试使用以下方法调用硒:

public void chooseChanelSource() {

        waitActions.sleep(1, TimeUnit.SECONDS);
        waitActions.waitUntilVisible(getSourceWebElement());
        ...
    }

但它总是给我相同的结果 - 元素不可见错误。

有什么提示吗?

编辑: 用户视角的网站代码(不再有 xyz 类):

<div class="center-content bg-grey">
<div class="operator-message">Some text</div>
<div class="row source-row">
    <span>Source:</span>
    <div class="sources">
        <button type="button" class="active" onclick="changeOperatorSource('PHONE', this);">Phone</button>
        <button type="button" class="" onclick="changeOperatorSource('EMAIL', this);">Email</button>
    </div>
</div>

默认情况下,第一个按钮处于活动状态。我想点击第二个(带有“EMAIL”标签的那个)。

【问题讨论】:

  • 在 Chrome 中打开页面并在 DevTool 中尝试 css 选择器 .xyz 以查看 css 选择器找到的第一个元素是您想要的按钮,并且是否可见。如果它不是您想要的按钮,请在下面的答案中使用@DebanjanB 提供的 css 选择器;如果可见,请增加等待超时。
  • 好吧,不幸的是,botk 解决方案对我没有任何改变。因为我点击哪个按钮并不重要(假设有 3 个或更多)我什至尝试了 @FindBy(css="button") private WebElement operatorSource;它也没有帮助。
  • 您给定的 html 代码实际上是从开发人员的角度来看的页面源代码。请从用户的角度给出 html 代码。为此,您可以使用浏览器的 DevTool 或浏览器的上下文菜单:'Inspect'。
  • 我更新了我的代码。

标签: java selenium wait


【解决方案1】:

您适应的定位器策略似乎不合适。您可以使用以下定位器策略

@FindBy(css="div.sources button.xyz[type='button']") private WebElement operatorSource;

此外,您需要将wait 时间段增加一点,以便 webelementHTML DOM 中呈现,如下所示:

waitActions.sleep(5, TimeUnit.SECONDS);

【讨论】:

    【解决方案2】:

    试试下面的代码 sn-p:

    @FindBy(css="div.sources button[onclick*='EMAIL']") 
    private WebElement operatorSourceEmail;
    
    @FindBy(css="div.sources button[onclick*='PHONE']") 
    private WebElement operatorSourcePhone;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 2017-11-20
      相关资源
      最近更新 更多