Selenium.click not working on some anchor elements

最后都变了- 提交于 2019-12-12 10:56:15

问题


The application that am working on was recently revamped and as part of that a new JQuery calendar was introduced. I need to click on a link in the calendar to select the time and date. However, Selenium.click is not working. The command gets executed, but nothing happens on the screen.

To check whether my XPATH/CSS locator (I tried both) is correct, I added selenium.getText(locator) and selenium.highlight(locator) commands. Both worked!. No issues in that. Its ONLY the click that is not working.

Upon checking in firebug, I could see that the div on which I am trying to click is kind of grayed out state. Does it meant that element is disabled? See the screenshot of the firebug below.

I also tried to run the same command in Selenium IDE. In IDE this works "sometimes".

I am running this test using Selenium 1.xx.

UPDATE:

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?

Has anyone faced a similar issue before? Any help would be appreciated!!!


回答1:


Try selenium.fireEvent(locater, 'click'), or using Selenium 2 which is more tightly integrated with the browser.

You may be having the same problem as some other people, eg.

Selenium clicks not working with GWT

Using Selenium to 'click' on non-input or non-control elements

It seems to be related to click events which are added with Javascript.

Edited

I don't know if you're using the same calendar implementation, but I discovered that the fullcalendar.js jQuery one replaces the mouseover event, and you have to trigger that first. I got it to work using

selenium.runScript("jQuery(\"a:contains('" + NEW_EVENT_NAME
        + "')\").trigger('mouseover');jQuery(\"a:contains('"
        + NEW_EVENT_NAME + "')\").trigger('click')");



回答2:


Our JQuery calendar implementation is compatible with the default Selenium locators, even though the locators appear disabled in the DOM. Here's an example, for you to try:

selenium.click("link=11:00 AM - 01:00 PM");



回答3:


Ok, I just assume your XPATH to get that span is correct, I suspect your selenium script is running faster than your page load, so add this function to wait until the page is loaded

waitForPageToLoad

Hope helps :)




回答4:


I've come across with the similar problem recently. Please note, I work with Selenium driver. So I'm not sure whether my approach is suitable for Selenium 1.xx

The problem was in clicking invisible menu elemnt , which appears on mouse hover event. The solution I found for Firefox selenium driver:

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

Here's the link

Main idea is to create instance of Actions and try to focus on your element and click it. I would act in the following way: //find xpath of your invisible element:

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

In firefox this works OK. But IE driver has problems with clicking on elements. So I overcome this "IE problem" using jscript directly in the following way:

WebElement hiddenWebElement =driver.findElement(By.xpath(....));
        ((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);

we initialize hiddenWebElement variable with element we want to click on. And using jscript we click on it.

Hope this helps you.




回答5:


I ran into a similar problem to yours (one anchor/link element out a hundred or so I'd tested before just fine refused to be clicked by a standard selenium click method).

In my case, I was able to solve the problem, for reasons entirely unclear to me, using Selenium WebDriver Actions (documented here for Java and here for Ruby) to execute a clickAndHold/click_and_hold action followed by a release action. In Ruby, this is done like so (code example from Ruby documentation of release):

el = driver.find_element(:id, "some_id")
driver.action.click_and_hold(el).release.perform

Hoping this helps someone else, and perhaps that someone with more insight can explain the reasons behind this.




回答6:


Do the following:

selenium.focus("locator path of where you want to click");
selenium.keyPressNative("10"); // this is clicking entering button

this should do the job.

Make sure that you don't touch the mouse while Selenium is executing your keyPressNative statement.



来源:https://stackoverflow.com/questions/6134708/selenium-click-not-working-on-some-anchor-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!