我尝试在网页上运行一些测试。我是初学者,到目前为止一切都很顺利。
我正在更改网络表单中的一些值,现在想要按“保存并退出”按钮。
但是当我使用 webdriver (driver.getPageSource();
) 查看可用源时,我没有看到按钮,只看到下面的 JavaScript。我将脚本缩短为一个按钮 - 我想单击的一个按钮。
function getToolbarCfg() {
return [{ btnId: 2, icon:'images/obj16/tsave_.gif', text:'Save & Exit', qtip:'Save Record and Exit', handler:function() { cwc.getCenterWindow().tpzExecute('2',null,'detail'); } }];
感谢任何帮助。
最佳答案
您可以让 WebDriver 显式等待元素出现。
public static IWebElement WaitForElementToAppear(IWebDriver driver, int waitTime, By waitingElement)
{
IWebElement wait = new WebDriverWait(driver, TimeSpan.FromSeconds(waitTime)).Until(ExpectedConditions.ElementExists(waitingElement));
return wait;
}
阅读Selenium's WebDriver Wait Documentation对显式和隐式等待的良好解释。
编辑:
您也可以这样做:
public static IWebElement WaitForElementToAppear(IWebDriver driver, int waitTime, By waitingElement)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(waitTime));
var element = wait.Until(d =>
{
var elem = driver.FindElement(waitingElement);
if (elem.Enabled)
return elem;
else return null;
});
return element;
}
这将每隔 0.5 秒 ping 一次元素,直到定义任何 waitTime 或为 OR 直到元素出现为止。
关于java - 当按钮似乎是从 JavaScript 生成时,如何使用 Selenium 访问按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24476722/