繁体   English   中英

无法单击 Selenium 中的按钮

[英]Unable to click a button in Selenium

我在点击 Selenium 的按钮时遇到了困难。

这是 DOM: http : //pasteboard.co/GHIjMd6.png

我已经使用 Xpath(根据 Firepath 返回一个节点的有效 Xpath)以这种方式声明了按钮 WebElement:

WebElement send_this_msg_btn = driver.findElement(By.xpath("//*[@class='mp-button-content'][.='Send This Message']"));

我已经尝试以send_this_msg_btn方式使用send_this_msg_btn Xpath 单击所述按钮,但它们都不适合我。

WebDriver 的click()方法: send_this_msg_btn.click()

JavaScript 执行器:

JavaScriptExecutor jse = (JavaScriptExecutor)driver;
jse.executeScript("arguments[0].click();", send_this_msg_btn);

动作类:

Actions actions = new Actions(driver);
actions.moveToElement(send_this_msg_btn);
actions.click();
actions.build().perform();

我还检查了按钮是否在框架/iframe 内,但事实并非如此。

将 xpath 更改为.//mp-button[@class='mp-button-primary submit_button']解决了这个问题,但我不确定这是否应该是公认的答案,因为我不确定它是否是脆弱的路径。 欢迎任何建议!

尝试单击父按钮

xpath = "//*[@class='mp-button-content'][.='Send This Message']/.."

或者是父母

xpath = "//*[@class='mp-button-content'][.='Send This Message']/../.."

并使用显式等待直到元素可点击。

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elem = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
elem.click();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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