如何与 ExtJS 消息框元素中的按钮进行交互?
How to interact with buttons in ExtJS's MessageBox element?
我有一个 Ext.MessageBox 当用户点击我想要的 url 上的按钮时出现。
Ext.MessageBox.confirm('Confirm', 'Are you sure you want to do that?',function(btnText){
if(btnText === "no"){
}
else if(btnText === "yes"){
// Do Something
}
}, this);
}
我想测试此消息 window 上的 'yes' 和 'no' 按钮。由于我没有为其设置 ID,谁能告诉我如何访问这些按钮?
我试过
WebElement y = driver.findElement(By.xpath("//a[2]/span/span/span[2]"));
但是没有用。我也检查了有关堆栈溢出的其他类似问题,但它对我没有用。
请帮忙。
我假设您正在谈论此类 MessageBox 元素:
由于 extjs 会即时生成元素 ID,因此查找元素 ID 并不容易。
不过还是有一些模式可以帮助您与按钮进行交互:
所以你需要做的是首先借助隐式等待定位元素然后点击它:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement yesButton = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.LinkText("Yes")));
yesButton.click();
我自己也找到了解决问题的方法。我利用 Chrome XPath Helper 扩展来找出这个 "yes" 按钮的 XPath 并像
一样使用它
WebElement yesButton = driver.findElement(By.xpath("/html/body/div/div/div/div/a[2]/span/span/span[2]"));
new WebDriverWait(driver, 5);
yesButton .click();
但是@Viktor 给出的解决方案是更干净的解决方案,我接受了他的最佳答案。
我有一个 Ext.MessageBox 当用户点击我想要的 url 上的按钮时出现。
Ext.MessageBox.confirm('Confirm', 'Are you sure you want to do that?',function(btnText){
if(btnText === "no"){
}
else if(btnText === "yes"){
// Do Something
}
}, this);
}
我想测试此消息 window 上的 'yes' 和 'no' 按钮。由于我没有为其设置 ID,谁能告诉我如何访问这些按钮?
我试过
WebElement y = driver.findElement(By.xpath("//a[2]/span/span/span[2]"));
但是没有用。我也检查了有关堆栈溢出的其他类似问题,但它对我没有用。 请帮忙。
我假设您正在谈论此类 MessageBox 元素:
由于 extjs 会即时生成元素 ID,因此查找元素 ID 并不容易。
不过还是有一些模式可以帮助您与按钮进行交互:
所以你需要做的是首先借助隐式等待定位元素然后点击它:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement yesButton = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.LinkText("Yes")));
yesButton.click();
我自己也找到了解决问题的方法。我利用 Chrome XPath Helper 扩展来找出这个 "yes" 按钮的 XPath 并像
一样使用它WebElement yesButton = driver.findElement(By.xpath("/html/body/div/div/div/div/a[2]/span/span/span[2]"));
new WebDriverWait(driver, 5);
yesButton .click();
但是@Viktor 给出的解决方案是更干净的解决方案,我接受了他的最佳答案。