java - 如何使用 Selenium 和 Java 单击文本为 Ok 的元素

标签 java selenium xpath css-selectors webdriverwait

<div id="RadWindowWrapper_alert1579252675818" class="RadWindow RadWindow_Metro rwNormalWindow rwTransparentWindow" unselectable="on" style="width: 398px; height: 148px; position: absolute; transform: none; backface-visibility: visible; touch-action: none; visibility: visible; left: 84px; top: 113px; z-index: 3012;"><table cellspacing="0" cellpadding="0" class="rwTable" style="height: 148px;"><tbody><tr class="rwTitleRow"><td class="rwCorner rwTopLeft">&nbsp;</td><td class="rwTitlebar" style="cursor: move;"><div class="rwTopResize"><!-- / --></div><table align="left" cellspacing="0" cellpadding="0" class="rwTitlebarControls"><tbody><tr><td style="width: 16px;"><a class="rwIcon"></a></td><td><em unselectable="on" style="width: 318px;">Automax Message</em></td><td nowrap="" style="white-space: nowrap;"><ul class="rwControlButtons" style="width: 32px;"><li><a href="javascript:void(0);" class="rwCloseButton" title="Close"><span>Close</span></a></li></ul></td></tr></tbody></table></td><td class="rwCorner rwTopRight">&nbsp;</td></tr><tr class="rwContentRow"><td class="rwCorner rwBodyLeft">&nbsp;</td><td class="rwWindowContent" valign="top"><iframe name="alert1579252675818" src="javascript:'<html></html>';" frameborder="0" style="width: 100%; height: 100%; border: 0px; display: none;"></iframe><div id="alert1579252675818_content" style="">
    <div class="rwDialogPopup radalert" style="background-image: url(&quot;../Images/InteractionImages/AutomaxSuccess.png&quot;);">         
        <div class="rwDialogText" id="alert1579252675818_message">
        Location added successfully<br><br>             
        </div>

        <div>
            <a onclick="$find('alert1579252675818').close(true);" class="rwPopupButton" href="javascript:void(0);" tabindex="-1">
                <span class="rwOuterSpan">
                    <span class="rwInnerSpan">Ok</span>
                </span>
            </a>                
        </div>
    </div>
    </div></td><td class="rwCorner rwBodyRight">&nbsp;</td></tr><tr class="rwStatusbarRow" style="display: none;"><td class="rwCorner rwBodyLeft">&nbsp;</td><td class="rwStatusbar"><table align="left" cellspacing="0" cellpadding="0" style="width: 100%;"><tbody><tr><td style="width: 100%;"><input id="alert1579252675818_status" readonly="" unselectable="on" tabindex="-1"><label for="alert1579252675818_status" style="display: none;">status label</label></td></tr></tbody></table></td><td class="rwCorner rwBodyRight">&nbsp;</td></tr><tr class="rwFooterRow"><td class="rwCorner rwFooterLeft">&nbsp;</td><td class="rwFooterCenter">&nbsp;</td><td class="rwCorner rwFooterRight">&nbsp;</td></tr></tbody></table></div>
  1. “确定”按钮消息的详细 HTML 标记。
  2. name="alert1579246138835"会在刷新页面时发生变化。
  3. 页面上存在/可见弹出窗口/警报的 HTML

最佳答案

所需的元素是动态元素,因此要在该元素上定位并 click(),您必须为 elementToBeClickable() 引发 WebDriverWait> 您可以使用以下任一 Locator Strategies :

  • css选择器:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.rwPopupButton > span.rwOuterSpan > span.rwInnerSpan"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='rwPopupButton']/span[@class='rwOuterSpan']/span[@class='rwInnerSpan' and text()='Ok']"))).click();
    

关于java - 如何使用 Selenium 和 Java 单击文本为 Ok 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59749423/

相关文章:

java - 卡片布局和对象?

java - 对集合框架的动态类型转换效果

python - 属性错误 : 'list' object has no attribute 'click' - Selenium Webdriver

cookies - 如何将 Scrapy 登录 cookie 传递给 Selenium?

javascript - Selenium 中的 Xpath

java - android获取相应日期的前一个日期(不是昨天的日期)

java - 为什么我遇到 java.util.InputMismatchException 问题?

JAVA Selenium WebElement 覆盖 click() 方法

java - 使用 Java 删除标签并更改 XML 中的一行?

java - 是否可以在java中使用xpath来获取其中包含HTML的cdata节点的内容