【问题标题】:Selenium webdriver c# - unable to click() element (element not null)Selenium webdriver c# - 无法点击()元素(元素不为空)
【发布时间】:2013-09-25 02:38:23
【问题描述】:

我似乎在单击由 ajax 填充的框内的元素时遇到问题。

所以我所在的网页上有一个链接,单击该链接时会调用一个 javascript 函数,然后将一个新的 div 插入到充满新内容的页面中。

现在奇怪的是我可以使用 xpath 找到这个框内的元素没有问题,我什至可以读取它的值但是!我不能使用 Click();在框内的链接上,该事件由于某种原因无法正常工作。

有没有人遇到过类似的问题并知道解决方法?

我在 Firefox 23 中使用 Selenium webdriver 2.35

 More Info

好的,所以我单击的链接的 HTML 调用 JS 使 div 出现。

<center>
    <a id="link_fleet_move_here" href="">Move fleet here</a>
</center>
<br>
<script>
    $("#link_fleet_move_here").click( function(event) { event.preventDefault(); load_fleet_move_to_destination("fleet.aspx?method=ajax&view=move_to_destination&version=1&player=111&destination=LZLOCATION"); $("#link_fleet_move_here").hide();} )
</script>
<center>
<div id="fleetLoaderTemplate" style="display:none">
<div id="fleetLoaderErrorTemplate" style="display:none">
</center>
<div id="move_to_destination_container"></div>

当事件完成加载新的 HTML

<div id="move_to_destination_container">
    <ajax>
        <table width="600" align="center">
        BIG TABLE FULL OF CONTENT
        <td sorttable_customkey="LZLOCATION">
            <a href="map.aspx?loc=LZLOCATION">(LZLOCATION)</a>
        </td>
        <td sorttable_customkey=""></td>
        <td sorttable_customkey=""></td>
        <td>
            <a href="fleet.aspx?fleet=&view=move&destination=AnotherLocation">Move</a>
        </td>
        <table>
    <br>
    </ajax>
</div>

选择器

location = driver.FindElement(By.XPath("//a[contains(@href, '" + LZLocation + "')]/following::td[3]"));
location.Click();

我认为这实际上可能与那个 div 有关,我认为它以 Display:None 开头并被更改,这会影响它吗?

我以为它是动态添加的,但可能不是!

【问题讨论】:

  • 我猜click() 会执行一些javascript。尝试通过 Actions API 或 javascript 执行点击。有时你必须做这样的解决方法。
  • 我不是在谈论使用 Javascript 的 Webdriver,而是说页面有一个附加到按钮的 javascript 事件。将驱动程序转换为 JavascriptExecutor,然后尝试从那里执行点击。
  • 嗯,不,我单击一个链接,该链接会触发一些 JS,并且 JS 会向页面添加一个新 div,然后我在这个新 div 中选择一个链接,我可以找到这个元素,甚至返回它的文本/值/属性,但 Click() 不会单击它。该元素只是一个 链接,指向未附加到任何 JS 的另一个页面。
  • 你能发布一些 HTML,以及你正在使用的选择器吗?可能是您选择了错误的元素。
  • ok 已发布,老实说,我认为它与 CSS 有关。但我想只要我能看到它,我就能看到它:/我的意思是它发现它没问题。

标签: c# ajax xpath selenium webdriver


【解决方案1】:

尝试通过以下方式选择您的元素:

driver.findElement(By.cssSelector("#move_to_destination_container a[href^='fleet']")).click();

如果抛出错误,请尝试使用:

new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#move_to_destination_container a[href^='fleet']"))).click();

【讨论】:

  • 很抱歉一直打扰您,但是点击会怎样?因为生成的 div 在表中包含大量链接,而我所做的 xpath 选择器是唯一能找到我想要单击的正确链接的东西。
猜你喜欢
  • 2016-01-18
  • 2018-03-14
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 2012-09-03
  • 2012-03-26
相关资源
最近更新 更多