将无效的文件类型添加到文件上传部分后,验证消息将显示为工具提示,即使在使用moveToElement()之后我也无法验证工具提示;
以下是我用来使工具提示可见的代码
`driver.findElement(By.xpath(""//input[@type='file']"").sendKeys(invalidLicenseFileType.txt);
Actions actions = new Actions(driver);
actions.moveToElement(//*[contains(@class, 'fieldlabel-content')]).build().perform();`
HTML代码:
<div class="fieldlabel-content">
<label class="fileinput fileinput--invalid">
<div class="fileinput-cell fileinput-cell-input">
<input placeholder="Choose a .license file..." accept=".license" value="" tabindex="0" type="file">
<div class="fileinput-description">
File:
<span class="fileinput-description-file">
<span class="icon fa fa-file-text-o"></span>
invalidLicenseFileType.txt
</span>
</div>
</div>
<div class = "popup popup--theme-error popup--position-top popup--hover" style="top: 131px; left: 399px;">Validation message for uploading invalid file type.</div>
</label>
答案 0 :(得分:1)
我有两个建议来解决这个问题:
<强> First : Using JavaScript
强>
WebElement element = driver.findElement(By.xpath("value of xpath")).sendKeys(invalidLicenseFileType.txt);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].onmouseover()", element);
在最后一行,代替element
,您可以指定要悬停的xpath of your WebElement
。
<强> Second : Using Actions
强>
尝试在Actions类中使用 clickAndHold
代替 moveToElement
。
WebElement element = driver.findElement(By.xpath("value of xpath")).sendKeys(invalidLicenseFileType.txt);
Actions actions = new Actions(driver);
actions.clickAndHold(//*[contains(@class, 'fieldlabel-content')]).build().perform();
答案 1 :(得分:0)
我认为问题是操作尝试将鼠标悬停在元素上,但该元素尚未显示。 你可以尝试下面的代码
driver.findElement(By.xpath("//input[@type='file']").sendKeys("invalidLicenseFileType.txt");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions. presenceOfElementLocated(By.cssSelector(".popup--theme-error")));
Actions actions = new Actions(driver);
actions.moveToElement("//*[contains(@class, 'fileinput-description-file')]").perform();
答案 2 :(得分:0)
我可以在你的代码中看到一些错误。
<强>代码:强>
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("invalidLicenseFileType.txt");
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//*[contains(@class,'fieldlabel-content')]"));
actions.moveToElement(element).build().perform();
String toolTipText = driver.findElement(By.xpath("//div[contains(@class,'popup popup--theme-error')]")).getText();
System.out.println("Tool tip text present :- " + toolTipText);
// Compare tool tip text
Assert.assertEquals("Validation message for uploading invalid file type.", toolTipText);
答案 3 :(得分:0)
实施以下代码有助于解决我的问题。
try
{
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript,uploadFileInputFieldWebElement);
Thread.sleep(1000);
invalidLicenseFileTypeMessag = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;",tooltipWebElement);
} catch (Exception e) {
exception message;
}