【问题标题】:Selenium webdriver script that runs in IE hangs in Firefox在 IE 中运行的 Selenium webdriver 脚本在 Firefox 中挂起
【发布时间】:2013-02-18 17:40:52
【问题描述】:

我过去使用过 selenium RC,但我是 webdriver 的新手。 我的应用程序中有三个链接。通知、消息和连接。 单击通知时,将显示通知下拉框。单击消息时,将显示消息下拉框,单击连接时,将显示连接下拉框。 在脚本中,我单击通知链接,等待通知下拉框,然后断言通知下拉框。消息和连接的顺序相同。 通知序列正常工作。在消息中,它单击消息链接,然后挂在等待消息下拉框链接上。我知道这一点,因为我在每一行之后都放了 print 命令。 这是我的代码:

 driver.findElement(By.xpath("//a[@id='notifications-page-button']")).sendKeys("\n");
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id='notifications-dropdown-list']//li//div[@class='message']")));
Assert.assertTrue(isElementPresent(By.xpath("//div[@id='notifications-dropdown-list']//li//div[@class='message']")), "Notifications drop box was not displayed");

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@id='messages-page-button']")));

driver.findElement(By.xpath("//a[@id='messages-page-button']")).sendKeys("\n");
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='sf_messages_list dropdown']//li//div[@class='message']"))); //This is the line where the script hangs. If I remove this line and the next line and continue with just the click commands, they work. But when I have this line and the next, the remaining click commands are not executed

Assert.assertTrue(isElementPresent(By.xpath("//div[@class='sf_messages_list dropdown']//li//div[@class='message']")), "Messages drop box was not displayed");

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("connections-page-button"))); 
driver.findElement(By.id("connections-page-button")).click()

这是消息部分的 HTML:

<li class="icon mm open">
<a id="messages-page-button" class="mm" href="#">
<span class="icon"></span>
<span class="badge hidden_elem">
<strong>
<em>0</em>
</strong>
</span>
</a>
<div class="dropdown-holder">
<div class="sf_messages_list dropdown" data-eventprefix="mainmenu_messages">
<div class="tb-dropdown-header">Messages</div>
<div class="tb-dropdown-body" data-url="/messages/dropdown">
<div class="document-title" style="display: none; !important"> - Dropdown Messages</div>
<div id="messages_list_view_76345" class="message-threads-listview">
<ul>
<div class="no_items hidden_elem">
</div>
</div>
<div class="tb-dropdown-footer" style="display: block;">
</div>
<span class="shadow-hide"></span>
</div>
</li>

这是通知部分的 HTML:

<li class="icon mm">
<a id="notifications-page-button" class="mm" href="#">
<span class="icon"></span>
<span class="badge hidden_elem">
<strong>
<em>0</em>
</strong>
</span>
</a>
<div class="dropdown-holder">
<div id="notifications-dropdown-list" class="sf_notifications_list dropdown" data-eventprefix="mainmenu_notifications">
<div class="tb-dropdown-header">Notifications</div>
<div class="tb-dropdown-body" data-url="/notifications/dropdown"></div>
<div class="tb-dropdown-footer">
<a class="view_all" href="/notifications/view">View All Notifications</a>
</div>
</div>
<span class="shadow-hide"></span>
</div>
</li>

以上代码适用于 IE。所以看起来问题不在于它无法找到元素。 我正在使用 Selenium 2.25.0。我尝试过各种版本的 FF,包括 3.6、7、11、13、15 和 16。但它们都不起作用。 此外,脚本只是挂起。它甚至不会在 Eclipse 中引发错误。我曾经让我的脚本运行了大约 11 个小时,但仍然没有错误。

如果您需要更多信息来帮助我解决此问题,请告诉我。

谢谢!

【问题讨论】:

  • 经历了和你一模一样的情况。无法弄清楚该怎么做..你解决了这个问题吗?还是有解决办法?请帮我....! :-(

标签: selenium webdriver selenium-webdriver


【解决方案1】:

在以前版本的 Selenium Webdriver 中,我也遇到过类似的情况。我也一无所知,发生在我身上的事情。最终,更新到最新版本的 Selenium 对我有好处,但由于 2.25.0 是最新的,我至少会向您展示我使用的解决方法,直到更新解决它

每当我需要点击一个按钮时,什么都没有发生(对你而言)。所以解决方法是,当点击按钮时,我也会向它发送Enter 键事件。

更具体一点:

 WebElement buttonWhereClickingDoesNotWork = driver.findElement(By.id("messages-page-button");
buttonWhereClickingDoesNotWork.click();
buttonWhereClickingDoesNotWork.sendKeys(Keys.ENTER);

是的,它的解决方法。是的,它不好看。是的,它确实帮助了我。

另外:不,我不知道这个问题的根本原因,因为 Selenium 的更新对我有帮助...

【讨论】:

  • 感谢您的回复。至少有人遇到过这种情况并且可以理解我的情况:)。我尝试了您建议的解决方法,但没有帮助。我添加了在单击命令后按回车的命令。但我使用的是 Selenium 2.25.0 和 FF 15。你认为我应该使用 Selenium 2.22 和 FF 13 或以前的版本来解决你的问题吗?
  • 我尝试降级到 Selenium 2.22 和 FF 13。尝试了您建议的解决方法,但没有奏效:(
  • 太糟糕了......这是我知道的唯一解决方法,它对我有用
【解决方案2】:

这似乎与 Facebook 登录有关。您是否在窗口句柄之间正确切换?另外你的隐式超时是多少,默认设置为 30 秒,所以我不确定你的脚本是否可以运行 11 小时而不会出错。

你可以试试这些

1) 我是猜测className("message") 不存在,您的脚本实际上卡在了该步骤而不是之后的步骤。其中涉及到点击。

driver.findElement(By.id("notifications-page-button")).click();
wait.until(driver.findElement(By.id("messages-page-button")));#<<-- Changed the element to wait for

//The code works untill here. But on the next command that clicks messages link, it hangs
driver.findElement(By.id("messages-page-button")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("username"))));

2) 移除等待元素

driver.findElement(By.id("notifications-page-button")).click();
#removed this wait for element

//The code works untill here. But on the next command that clicks messages link, it hangs
driver.findElement(By.id("messages-page-button")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("username"))));

更新


请试试这个...

driver.findElement(By.id("notifications-page-button")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("message"))));

//The code works untill here. But on the next command that clicks messages link, it hangs
driver.findElement(By.id("messages-page-button")).click();
wait.until(driver.findElement(By.id("connections-page-button"))); # changed it to wait for the element that you will next work with

driver.findElement(By.id("connections-page-button")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("connections-listview"))));

另外,如果您提供网页元素的 HTML sn-p,但您不确定定位器,我们可以帮助解决。

【讨论】:

  • 是的。切换正确发生。它甚至在登录后执行主窗口上的第一个单击命令。但在那之后,什么也不做。如果隐式等待是 30 秒,那么应该就是这样。另外,我指定的等待参数是 30 秒。
  • 感谢您的回复。不幸的是,我尝试了您的两个建议,但没有成功:(
  • 没问题,您可以输入一些打印命令来确认您实际卡住的步骤并更新问题
  • 它卡在尝试单击消息按钮的位置。我将代码修改为:driver.findElement(By.xpath("//a[@id='notifications-page-button']")).click(); System.out.print("Notification button click passed"); driver.findElement(By.id("messages-page-button")).click(); System.out.print("Message button click passed"); 在控制台中我看到:通知按钮单击通过。
  • 这似乎有帮助!!!但问题是,如果我等待消息元素然后添加 Thread.sleep 脚本不会点击消息元素。例如driver.findElement(By.xpath("//a[@id='notifications-page-button']")).click(); wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@id='messages-page-button']"))); Thread.sleep(2000); driver.findElement(By.xpath("//a[@id='messages-page-button']")).click();此代码不起作用。但是当我删除睡眠时,它会起作用。所以我认为它希望我立即点击我正在等待的元素。
猜你喜欢
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 2013-08-31
  • 2012-01-08
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 2012-04-29
相关资源
最近更新 更多