Selenium.click not working on some anchor elements
正在处理的应用程序最近经过了修订,并且作为其中的一部分,引入了新的JQuery日历。 我需要单击日历中的链接以选择时间和日期。 但是,Selenium.click无法正常工作。 命令被执行,但是屏幕上没有任何反应。
为了检查我的XPATH / CSS定位器(我都尝试过)是否正确,我添加了selenium.getText(locator)和selenium.highlight(locator)命令。 两者都有效! 没问题。 它只有不起作用的点击。
检入Firebug后,我可以看到要尝试单击的div处于灰色状态。 这是否意味着该元素已禁用? 请参阅以下萤火虫的屏幕截图。
我也尝试在Selenium IDE中运行相同的命令。 在IDE中,此功能"有时"起作用。
我正在使用Selenium 1.xx运行此测试。
更新:
I did one more thing as part of
debugging. During the test run, I
opened the Selenium IDE in the browser
so that it records what actions are
happening. IDE recorded all actions
till this click. But I couldn't see
anything in the IDE when the click
command was executed. Any idea guys,
what would be cause?
有人遇到过类似的问题吗? 任何帮助将不胜感激!!!
尝试
您可能和其他人一样遇到同样的问题,例如。
硒点击不适用于GWT
使用Selenium单击非输入或非控制元素
它似乎与随Javascript添加的点击事件有关。
已编辑
我不知道您是否使用相同的日历实现,但是我发现fullcalendar.js jQuery替换了mouseover事件,您必须首先触发该事件。我使用它来工作
1 2 3 | selenium.runScript("jQuery(\"a:contains('" + NEW_EVENT_NAME +"')\").trigger('mouseover');jQuery(\"a:contains('" + NEW_EVENT_NAME +"')\").trigger('click')"); |
我遇到了一个与您类似的问题(一个锚定/链接元素已被测试了一百个左右,然后才通过标准的硒单击方法拒绝单击)。
就我而言,由于我完全不清楚的原因,我可以使用Selenium WebDriver Actions(在此为Java,在此为Ruby记录)执行clickAndHold / click_and_hold操作,然后执行释放操作,以解决该问题。在Ruby中,这样做是这样的(来自Ruby文档的代码示例):
1 2 | el = driver.find_element(:id,"some_id") driver.action.click_and_hold(el).release.perform |
希望这对其他人有所帮助,也许有更多见识的人可以解释其背后的原因。
我最近遇到过类似的问题。请注意,我使用Selenium驱动程序。所以我不确定我的方法是否适合Selenium 1.xx
问题在于单击了鼠标悬停事件上出现的不可见菜单elemnt。
我为Firefox硒驱动程序找到的解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 | WebElement mnuElement; WebElement submnuElement; mnEle = driver.findElement(By.Id("mnEle")).Click(); sbEle = driver.findElement(By.Id("sbEle")).Click(); Actions builder = new Actions(driver); // Move cursor to the Main Menu Element builder.MoveToElement(mnEle).Perform(); // Giving 5 Secs for submenu to be displayed Thread.sleep(5000L); // Clicking on the Hidden SubMenu driver.findElement(By.Id("sbEle")).Click(); |
这是链接
主要思想是创建Actions实例,并尝试着重于您的元素并单击它。
我将以以下方式采取行动:
//找到不可见元素的xpath:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | String xpathInvisible ="//*[id="calendar"]/div/div/div/div[1]"; //find xpath of the element, on hovering which your invisible (inactive) element appear. I mean somthing //like VDIs (see my screen) on pressing which menu elements appear. String xpathCalendarToAppear ="....."; WebElement calendarToAppear= driver.findElement(By.xpath(xpathCalendarToAppear)); WebElement invisibleElement=driver.findElement(By.xpath(xpathInvisible)); Actions builder = new Actions(driver); builder.MoveToElement(calendarToAppear).Perform(); // Giving 5 Secs for submenu to be displayed Thread.sleep(5000L); // Clicking on the Hidden SubMenu invisibleElement.Click(); |
在Firefox中,这可以正常工作。但是IE驱动程序在单击元素时存在问题。
因此,我可以通过以下方式直接使用jscript克服此" IE问题":
1 2 | WebElement hiddenWebElement =driver.findElement(By.xpath(....)); ((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement); |
我们使用要单击的元素初始化hiddenWebElement变量。
然后使用jscript单击它。
希望这对您有所帮助。
请执行下列操作:
1 2 | selenium.focus("locator path of where you want to click"); selenium.keyPressNative("10"); // this is clicking entering button |
这应该做的工作。
确保在Selenium执行
好的,我只是假设您的XPATH获取的跨度是正确的,我怀疑您的selenium脚本的运行速度比您的页面加载速度快,因此请添加此功能以等待页面加载完毕
1 | waitForPageToLoad |
希望有所帮助
:)
我们的JQuery日历实现与默认的Selenium定位器兼容,即使该定位器在DOM中似乎已被禁用。这是一个示例,供您尝试:
1 | selenium.click("link=11:00 AM - 01:00 PM"); |