我正在尝试使用 selenium 设置 span 元素的内容,但似乎没有将其设置为值。 span 字段是一个值,在用户输入时从列表中填充。
问题的更多细节
1)转到http://www.goeuro.es . 2)尝试将目标字段设置为: a) 如果用户在目标字段中键入 berlin,它将被文本替换(这是从他们的 js 中填充的)
柏林阿勒曼尼亚
我尝试使用以下代码进行设置。
@FindBy(xpath = ".//*[@id='$desktopSearchform']/div[1]/div[2]/d-departure-position/div/div/div/span[1]")
private WebElement fromSearchFieldSpan1;
@FindBy(xpath = ".//*[@id='$desktopSearchform']/div[1]/div[2]/d-departure-position/div/div/div/span[2]")
private WebElement fromSearchFieldSpan2;
.
.
.
.
((JavascriptExecutor)driver).executeScript("arguments[0].innerHTML = 'Berlín'", fromSearchFieldSpan1);
((JavascriptExecutor)driver).executeScript("arguments[0].innerHTML = 'Alemania'", fromSearchFieldSpan2);
当我尝试使用上面提供的代码时,它似乎将文本设置为所述字段的标题,因为我在提交页面时收到输入验证错误(好像根本没有输入这些值)。 附上截图。
我无法使用 sendKeys() 设置此字段的值,因为这不是文本字段。
有没有想过如何使用 selenium 网络驱动程序来设置它?
最佳答案
您正在尝试处理 span
元素,而您应该处理 input
。试试下面的代码:
@FindBy(xpath = "//input[@id='$city']")
private WebElement fromSearchFieldSpan;
fromSearchFieldSpan.sendKeys('Berlin Alemania')
关于html - 设置 span 元素的内容,使用 selenium 从列表中填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42371099/