【问题标题】:Selenium Firefox driver passes test but doesn't actually click buttonSelenium Firefox 驱动程序通过测试但实际上并没有点击按钮
【发布时间】:2017-01-02 03:03:15
【问题描述】:

我正在使用 Selenium 和 Cucumber 在 Firefox 和 Chrome 中进行驱动测试。这些测试在 Chrome 中运行良好。但是,在 Firefox 中,我遇到了一个有趣的问题。下面场景中的每一步都表示它通过了:

Feature: Login Action

Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User enters "admin" and "password"
Then Message displayed Login Successfully

Scenario: Successful LogOut
When User LogOut from the Application
Then Message displayed LogOut Successfully
And I close the browser

但是,当它到达“用户从应用程序注销”时,它实际上从未注销。它说该按钮已被单击,但我仍然登录。

@When("^User LogOut from the Application$")
public void user_LogOut_from_the_Application() throws Throwable {

    driver.findElement(By.id("titlebar_hyperlink_8-lbsignout")).click();
    System.out.println("The click worked");
}


@And("^I close the browser$")
public void i_close_the_browser() throws Throwable {

    System.out.println("Driver= " + driver);
    driver.close();
    System.out.println("The browser should be closed");
}

它执行最后的and,但浏览器没有关闭,我仍然登录到网站。

该元素的结构如下所示(请原谅我从页面源复制和粘贴的格式)

<table role="presentation" align="right" style="height: 100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            <span id="titlebar-tb_username" class="homeButtontxtappname userfullname" style="display:none">MAXADMIN</span>
        </td>    
        <td id="titlebar_bulletinboardlink_3-co_0"          role='presentation'      nowrap="nowrap"      style="vertical-align:top;">

            <span id="titlebar_bulletinboardlink_3-lbshowbboard" align='left' ctype="label"    tabindex="0"      targetid="titlebar_bulletinboardlink_3-lbshowbboard"  mxevent="click" accesskey='B' class="text powerwhite   anchor" style="display:block;cursor:pointer;" title="Bulletins: &#040;0&#041;  ALT+B">
                <img id='titlebar_bulletinboardlink_3-lbshowbboard_image' src='btn_bboard.gif' class='pwimg' border='0'  style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
                <span>
                    <span/>
                    <span class='text hl hlak'>B</span>
                    <span>ulletins: (0) </span>
                </span>
            </span>

        </td>
        <td id="titlebar_hyperlink_5-co_0" role='presentation'      nowrap="nowrap"   align="left"   style="vertical-align:top;">

            <span id="titlebar_hyperlink_5-lbshowmenu_reportsmenu" align='left' ctype="label"    tabindex="0"    ev="reportsmenu"  targetid="titlebar_hyperlink_5-lbshowmenu_reportsmenu"  mxevent="click" accesskey='R' class="text powerwhite   anchor" style="display:block;cursor:pointer;" title="Reports ALT+R">
                <img id='titlebar_hyperlink_5-lbshowmenu_reportsmenu_image' src='btn_reporting.gif' class='pwimg' border='0'  style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
                <span>
                    <span/>
                    <span class='text hl hlak'>R</span>
                    <span>eports</span>
                </span>
            </span>
        </td>
        <td id="titlebar_hyperlink_7-co_0" role='presentation'             nowrap="nowrap"   align="left"   style="vertical-align:top;">
            <span id="titlebar_hyperlink_7-lbshowmenu_profile" align='left' ctype="label"    tabindex="0"    ev="profile"  targetid="titlebar_hyperlink_7-lbshowmenu_profile"  mxevent="click" accesskey='P' class="text powerwhite   anchor" style="display:block;cursor:pointer;" title="Profile ALT+P">
                <img id='titlebar_hyperlink_7-lbshowmenu_profile_image' src='btn_profile.gif' class='pwimg' border='0'  style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
                <span>
                    <span/>
                    <span class='text hl hlak'>P</span>
                    <span>rofile</span>
                </span>
            </span>
        </td>
        <td id="titlebar_hyperlink_8-co_0" role='presentation'      nowrap="nowrap"   align="left"   style="vertical-align:top;">
            <span id="titlebar_hyperlink_8-lbsignout" align='left' ctype="label"    tabindex="0"      targetid="titlebar_hyperlink_8-lbsignout"  mxevent="click" accesskey='S' class="text powerwhite   anchor" style="display:block;cursor:pointer;" title="Sign Out ALT+S">
                <img id='titlebar_hyperlink_8-lbsignout_image' src='btn_signout.gif' class='pwimg' border='0'  style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
                <span>
                    <span/>
                    <span class='text hl hlak'>S</span>
                    <span>ign Out</span>
                </span>
            </span>
        </td>
        <td id="titlebar_hyperlink_9-co_0" role='presentation'      nowrap="nowrap"   align="left"   style="vertical-align:top;">

            <span id="titlebar_hyperlink_9-lbshowmenu_help" align='left' ctype="label"    tabindex="0"    ev="help"  targetid="titlebar_hyperlink_9-lbshowmenu_help"  mxevent="click" accesskey='H' class="text powerwhite   anchor" style="display:block;cursor:pointer;" title="Help ALT+H">
                <img id='titlebar_hyperlink_9-lbshowmenu_help_image' src='btn_help.gif' class='pwimg' border='0'  style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
                <span>
                    <span/>
                    <span class='text hl hlak'>H</span>
                    <span>elp</span>
                </span>
            </span>
        </td>

    </tr>
</table>

我没有收到任何错误,它说我通过了,但我仍然没有退出。

编辑:我尝试使用以下解决方案,但没有成功

WebElement element = driver.findElement(By.id("titlebar_hyperlink_8-lbsignout_image"));
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();", element);
    wait.until(
            ExpectedConditions.visibilityOfElementLocated(By.
                    xpath("//img[@id='titlebar_hyperlink_8-lbsignout_image']")));

这是来自 Chrome 控制台的日志。

 document.getElementById("titlebar_hyperlink_8-lbsignout_image")
    <img id=​"titlebar_hyperlink_8-lbsignout_image" src=​"btn_signout.gif" class=​"pwimg" border=​"0" style=​"vertical-align:​top;​margin:​0px;​margin-left:​3px;​margin-right:​3px;​" alt>​
    document.getElementById("titlebar_hyperlink_8-lbsignout_image").click
click() { [native code] }
    document.getElementById("titlebar_hyperlink_8-lbsignout").click();
undefined
    document.getElementById("titlebar_hyperlink_8-lbsignout_image")
<img id=​"titlebar_hyperlink_8-lbsignout_image" src=​"btn_signout.gif" class=​"pwimg" border=​"0" style=​"vertical-align:​top;​margin:​0px;​margin-left:​3px;​margin-right:​3px;​" alt>​
document.getElementById("titlebar_hyperlink_8-lbsignout_image").click();
undefined

【问题讨论】:

  • 你确定提供的 chrome 浏览器和 firefox 浏览器的 ID 相同吗??
  • @SaurabhGaur 你能澄清一下吗?我使用完全相同的代码在两个浏览器中运行它。在初始化驱动程序的这一行之上,我只需将其从 Firefox 更改为 Chrome 并以完全相同的方式运行其他所有内容。 HTML ID 在浏览器之间发生变化应该没有任何原因。
  • @CeilingGecko 我研究了那个解决方案,但是提供的解决方案对我不起作用。
  • 确定提供的元素在两个浏览器上都是可点击的,只需通过按 f12 进入两个浏览器控制台并执行此脚本document.getElementById("titlebar_hyperlink_8-lbsignout").click() 并让我知道 click() 在两个浏览器上是否适用于同一元素..

标签: java html selenium firefox cucumber


【解决方案1】:

经过多次测试,我设法找到了答案。虽然 titlebar_hyperlink_8-lbsignout 在 Chrome 中用作选择器,但我显然不得不在 firefox 上更深入地一步,它现在可以工作了。

//set the wait driver inside local scope just to be safe. 
wait = new WebDriverWait(driver, 60); 
     Thread.sleep(3000);
    wait.until(ExpectedConditions
            .elementToBeClickable(By.id("titlebar_hyperlink_8-lbsignout_image")));
    System.out.println(driver.findElement(By.id("titlebar_hyperlink_8-lbsignout_image")));
    driver.findElement(By.id("titlebar_hyperlink_8-lbsignout_image")).click();
    System.out.println("This is where I should have pressed the logout button");

【讨论】:

    猜你喜欢
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2016-02-11
    • 1970-01-01
    • 2023-01-24
    • 2012-09-02
    • 2023-04-09
    相关资源
    最近更新 更多