簡體   English   中英

Selenium Webdriver C#-無法click()元素(元素不為null)

[英]Selenium webdriver c# - unable to click() element (element not null)

我似乎在單擊由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開頭並被更改,這會產生影響嗎?

我以為它是動態添加的,但可能不是!

嘗試通過以下方式選擇元素:

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM