【发布时间】:2020-08-22 03:35:00
【问题描述】:
我正在为一个角度/清晰度应用程序编写自动化测试,我想首先
键入
将一些文本放入 input 字段,然后从 datalist 中选择一个 option。
将文本设置为输入字段可以正常工作,但尝试单击
option 元素时出现以下异常:
Caused by: org.openqa.selenium.JavascriptException: javascript error: option element is not in a select
我已经尝试过用 selenium 和 javascript 来点击它。以下是两者的代码。
@Override
public void click(String xpath, MouseButton button, ClickPosition position)
{
LOG.info("Clicking on element '{}' with Mouse Button '{}' on Position '{}'!", xpath, button, position);
final WebElement element = find(xpath);
//in case we have a standard click (left on center) we spare out the whole calculation and just use standard click
if (button == MouseButton.LEFT && position == ClickPosition.CENTER)
{
element.click();
}
else
{
final Dimension size = element.getSize();
// opposite to the javadoc, the Actions#moveToElement() method is relative to the center and not to the top left corner with selenium api version 3.14.0
// REMARK: there is an issue currently filed so may be changed back at some point
final Point relativePoint = position.calculateRelativePointFromCenter(size.width - 1, size.height - 1);
performActions(a -> {
try
{
a.moveToElement(element, relativePoint.x, relativePoint.y);
switch (button)
{
case LEFT:
a.click();
break;
case RIGHT:
a.contextClick();
break;
case MIDDLE:
default:
throw new UnsupportedOperationException("Unexpected click button: " + button);
}
}
catch (final WebDriverException e)
{
if (e.getMessage().contains("Element is not clickable"))
{
throw new ElementNotClickableException("Unable to click on element with XPath '" + xpath + "'",
e);
}
}
});
}
}
@Override
public void javascriptClick(String xpath)
{
LOG.info("Triggering click event on element '{}' via javascript!", xpath);
parent.executeJavascript("arguments[0].click();", find(xpath));
}
System info: host: 'XXX', ip: 'XXX', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_191'
Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {browserName: chrome, browserVersion: 84.0.4147.125, chrome: {chromedriverVersion: 84.0.4147.30}
有人知道这里出了什么问题吗?
【问题讨论】:
-
表面上看起来不错 - 你的
string xpath的价值是多少? - 当您尝试单击时,selenium 会抛出什么错误? - 关于 Angular 的一个值得注意的事情是 selenium 难以处理的异步调用。你有没有在你的发现周围有一个 webdriverwait 来确保你的元素存在并准备好? -
xpath 字符串是 //OPTION[contains(@value, '%s')] (其中 %s 将被替换为项目的名称),是的,我们使用 webdriverwait我们的 find 方法。上面的错误几乎就是我们得到的一切。
-
由于您的选项元素包含在 datalist 标记中,您可以尝试单击数据列表而不是选项。 xpath = //datalist[option[contains(@value, '%s')]]。还使用 java 脚本执行器来单击而不是 element.click()。 JavascriptExecutor js= (JavascriptExecutor); js.executeScript("arguments[0].click();", element);
标签: selenium selenium-webdriver selenium-chromedriver