简体   繁体   English

Selenium Webdriver C#-无法click()元素(元素不为null)

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

I seem to be having issues clicking a element inside a box which is filled by ajax. 我似乎在单击由Ajax填充的框内的元素时遇到问题。

So the web page I am on has a link on it which when clicked this calls a javascript function which then inserts a new div into the page which is full of new content. 因此,我所在的网页上有一个链接,单击该链接时会调用javascript函数,然后该函数将一个新的div插入到包含新内容的页面中。

Now the weird thing is I can find the element inside this box no problem using xpath, and I can even read its value but! 现在很奇怪的是,我可以使用xpath在此框内找到元素没有问题,而且我什至可以读取它的值! I can't use Click(); 我不能使用Click(); on the link inside the box the event just wont work for some reason. 在框内的链接上,由于某种原因该事件将无法正常工作。

Has anyone faced a similar issue and know a work around? 有谁遇到过类似的问题并知道可以解决的方法?

I am using Selenium webdriver 2.35 with Firefox 23 我在Firefox 23中使用Selenium webdriver 2.35

 More Info

OK so the HTML for the link I click which calls the JS to make the div appear. 好的,所以单击链接的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>

And when the event finishes loading the new HTML 当事件完成后,加载新的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>

The Selector 选择器

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

I think it may be something to do with that div actually, I think it starts as Display:None and gets changed, this will be effecting it? 我认为实际上可能与该div有关,我认为它以Display:None开头并被更改,这会产生影响吗?

I thought it was dynamically adding it but maybe not! 我以为它是动态添加的,但可能不是!

Try selecting your element via the following: 尝试通过以下方式选择元素:

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

If it throws an error, try using: 如果抛出错误,请尝试使用:

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