简体   繁体   中英

Unable to click on webelement button

Unable to click on webelement button

I tried to click on button by mouse movement but no success

my outer html is as below :

<button class="btn btn-alt btn-small" type="button" ng-click="ecdapp.uploadBlueprintModalPopup();"> 
    Create
</button>

button xpath is:

//*[@id="page-content"]/div[3]/button

Not seeing the full page source it's hard to tell where your XPath expression is good or not, you can try locating the button using its text instead

//button[normalize-space(text())='Create']

在此处输入图片说明

the normalize-space() function is used to discard heading/trailing whitespaces

It might also be the case the button is not immediately available, I would recommend considering using Explicit Wait approach via WebDriverWait class

WebElement myButton = new WebDriverWait(driver, 10)
        .until(ExpectedConditions
                .elementToBeClickable(By.xpath("//button[normalize-space(text())='Create']")));
myButton.click();

the above code will try to locate the aforementioned button for 10 seconds and click it as soon as it will be present/visible/clickable. Otherwise it will fail with NoSuchElementException

May be the Xpath is wrong. Try the below xpath:

//button[contains(text(),'Create')]

As you can see on the screenshot this Xpath 100% works, if you still won't be able to click on that button, then problem is not with xpath. Let me know if its still fails.

 By.xpath("//button[@class = 'btn btn-alt btn-small' and @type = 'button']")

在此处输入图片说明

Based on your comment:

I tried this code , but unable to click . element click intercepted: Element ... is not clickable at point (293, 97). Other element would receive the click: ... (Session info: chrome=74.0.3729.169)

I pretty sure I know whats your problem, before u click on this element, something going on the page: It says - Other element would receive the click , means there is other element above(overlapping) your button(pop up window, page is greyed out(disabled while loading, Some JS running)), so when Selenium trying to click on your button its actually clicking on that blocking element. Try to click after Thread.Sleep(); wait 5-10 sec. If this is the case then u need to add condition before find your button to check that element that prevent from clicking on button is disappeared then u click on it.

Try JavaScript executors as below,

WebElement element = driver.findElement(By.xpath("<xpath of button>"));
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element); 

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