Home > database >  Wont click on this button with collapsible moves using Selenium with Java
Wont click on this button with collapsible moves using Selenium with Java

Time:10-06

I got this button:

enter image description here

It has this:

<strong data-bind="css: {'admin__collapsible-title': collapsible,
                      title: !collapsible,
                      '_changed': changed,
                      '_loading': loading,
                      '_error': error}" >
            <span data-bind="i18n: label">Sales Matrix</span>
            <!-- ko if: collapsible --><span >
                <span >
                    <span ></span>
                    <span  data-bind="i18n: 'Changes have been made to this section that have not been saved.'">Changes have been made to this section that have not been saved.</span>
                </span>
                <span >
                    <span ></span>
                    <span  data-bind="i18n: 'This tab contains invalid data. Please resolve this before saving.'">This tab contains invalid data. Please resolve this before saving.</span>
                </span>
                <span >
                    <span >
                       <!-- ko repeat: 8 --><span data-repeat-index="0"></span><span data-repeat-index="1"></span><span data-repeat-index="2"></span><span data-repeat-index="3"></span><span data-repeat-index="4"></span><span data-repeat-index="5"></span><span data-repeat-index="6"></span><span data-repeat-index="7"></span><!-- /ko -->
                    </span>
               </span>
            </span><!-- /ko -->
        </strong>

It's XPATH is:

//*[@id="container"]/div/div[2]/div[4]/div[1]/strong

I tried to do something like this:

driver.findElement(By.xpath("//*[@id=\"container\"]/div/div[2]/div[4]/div[1]/strong")).click();

but it says it couldnt found something with "strong".

Also, i tried this:

driver.findElement(By.linkText("Sales Matrix")).click();

But it wont work.


I solved it by doing this:

WebElement element = driver.findElement(By.xpath("//span[contains(text(),'Sales Matrix')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

CodePudding user response:

use the below xpath

//span[contains(text(),'Sales Matrix')]

OR

//span[normalize-space()='Sales Matrix']

OR

you can use elementToBeClickable(..) this will judge the element visible and enabled, so you can click on it.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); 
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("above-expath")));
element.click();

import

import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
  • Related