无法使用Selenium和Java单击按钮。它在 html 中"style=display: none",导致问题



我尝试过不同的xpath等,但无法在应用程序中单击"继续"按钮。以下是处理相同问题的尝试之一:-

WebDriverWait wait2 = new WebDriverWait(driver,20);
WebElement continueBtn = wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@type = 'submit' and @class = 'btn btn--primary']")));

当我检查元素时,我发现其中有以下详细信息:-

<button class="btn btn--primary" type="submit" data-bind="enable: !processing() &amp;&amp; !$root.accountLocked()">
<svg xmlns="http://www.w3.org/2000/svg" class="icon shape--loader" style="display: none;" viewBox="0 0 16 16" focusable="false" data-bind="visible: processing()">
<title>Processing</title>
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#shape--loader" />
</svg> 
Continue
</button>

引发的错误如下:-线程"main"org.openqa.selenium.TimeoutException:等待元素可点击20秒后超时:By.xpath://*[@type='submit'和@class='btn-btn-primary']

但是,对象/元素已经加载,所以不确定为什么不能单击该元素。

我也尝试过JavaScript执行器,但没有成功。

请帮我解决。提前谢谢!!!

澄清一下,上面给出的XPath是唯一的吗?如果是的话,你有没有尝试过JSExecutor?

尝试使用以下代码:

WebElement element = driver.findElement(By.xpath("//*[@type = 'submit' and @class = 'btn btn--primary']")));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

如果很难找到具有属性的元素,则根据按钮名称找到它。如果页面中只有一个"继续"按钮。

WebElement continueBtn = wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Continue')]")));

最后我得到了解决方案。在找到解决方案之前,我尝试了显式等待,我已经粘贴了有问题的代码,然后尝试使用JavaScript执行器,但它也不起作用。以下是解决方案:-

Actions action = new Actions(driver); 
action.sendKeys(Keys.ENTER).build().perform();

它运行得很好。感谢所有付出时间和努力的人。

最新更新