简体   繁体   中英

Selenium Drivers click on elements but does not redirect to next page

functionName is the linktext i need to click, the script executes the click but it does not redirect. Same script redirects when executed on other system.

wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText(functionName)));

        WebElement functionAnchor = driver.findElement(By.linkText(functionName));
        try {
            Thread.sleep(500);
            functionAnchor.sendKeys(Keys.ENTER);
            Thread.sleep(1000);
        } catch (ElementNotVisibleException e) {
            // Wait for stabilizing the rendered page
            Thread.sleep(500);
            // Scroll Into View the function link and perform click to avoid
            // "Element Not Visible for clicking" error
            ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", functionAnchor);
            functionAnchor.click();
            Thread.sleep(500);
        }

as you can see in the pic first i need to click on "More..." link which is working and it opens up the panel in which i need to click on purchase requisition

Analysis:

There are couple possible reasons which can lead to your issue:

  1. The page or the area where the link inside are loaing when you click on it, at that time point browser is busy on rendering element to page, it has no idle resource to react your click.

  2. The page or the area where the link inside are loaing when you click on it, developer use javascript to register click event to the link in page loading process, at that time point, the javascript snippet have not executed yet or not all compelte which lead the click event not register to the link, so there is no click event to react your click.

The reason why you can pass on other system, the possbile reason is browser more fast, network more fast, etc.

Solution:

Add sleep() before click and run multiple times on each system, when all pass, it hints your wait is not sufficient, at least on this system. Remove sleep() and modify wait condition to make wait can more close to end of page loading

The documentation clearly mentions that the ExpectedConditions clause which you are using ie visibilityOfElementLocated takes care of the expectation for checking that an element is present on the DOM of a page and visible but it doesn't covers if the element is Displayed and Enabled .

Next as we are trying to invoke click() method on the element so instead of visibilityOfElementLocated we will be using elementToBeClickable .

  • Element is Clickable - it is Displayed and Enabled.

If we use the ExpectedConditions clause as elementToBeClickable , instead of using sendKeys(Keys.ENTER) we can directly invoke the click() method and get rid of the Thread.sleep(500) too.

I happen to run into click() problems myself too.

Which browser and which version are you using?

Firefox and Internet Explorer has this problem with CLICK and I used JavaScript to click on the found web element.

WebElement element = driver.findElement(By.id("webElementId"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

I was facing an issue, where it was clicking an element but it was not redirecting to the next page and was not getting an exception also. I tried with Actions class, Javascript executor, Robot class and all the wait statements, but I was not getting the result. This solution worked for me.

try {
      //Next page element
    driver.findElement(By.xpath("//div[@class='web']/label/span[contains(text(), 'Congratulations!')]")).click();
        } catch (Exception e) {
    //current page element which is not redirecting after performing click operation
            driver.findElement(By.xpath("//div[@class='form-main-container']/button")).click();
        }

You have to try to interact with an element on the next page and use the XPath for the element in the try block and the XPath for the element which is not redirected to next page in a catch block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM