2017-06-23 95 views
0

向文件上传部分添加无效文件类型后,验证消息将显示为工具提示,即使在使用moveToElement()后,我也无法验证工具提示。Selenium moveToElement()读取工具提示不起作用

这里是我用来做工具提示可见的代码

`driver.findElement(By.xpath(""//input[@type='file']"").sendKeys(invalidLicenseFileType.txt); 
Actions actions = new Actions(driver); 
actions.moveToElement(//*[contains(@class, 'fieldlabel-content')]).build().perform();` 

ToolTip validation message image

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

你能告诉我们HTML吗? – Buaban

+0

@BuabanI已将HTML添加到问题 – SKV

+0

您可以在操作之前向我们提供错误消息和更多的java代码吗? – Buaban

回答

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; 

} 
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(); 
+0

它不适用于我Buaban – SKV

+0

@SujaiKrishna然后是什么错误? – Buaban

+0

它不能在元素上执行鼠标悬停功能 – SKV

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

尝试代替moveToElement在操作类使用clickAndHold

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(); 
+0

我尝试了两个选项,但我仍然无法集中元素,并且由于我无法从隐藏元素中获取工具提示文本 – SKV

+0

好的,请检查html中的任何框架更改或页面更改 –

+0

没有框架或者在我们输入文件时发生页面更改,并且如果文件类型立即失效,我会将验证消息作为工具提示。工具提示div是隐藏的,直到我将鼠标移到输入文件上。 – SKV

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);

+0

我使用与您在我的项目中提供的完全相同的代码。由于某种原因,moveToElement()函数不起作用,因此,在测试运行时找不到隐藏的元素,因此工具提示文本未被检索到。 – SKV

+0

您是否完全复制了我提供的代码并再次尝试。它应该工作。我可以在您提供的代码中看到一些错误。一些语法问题。 – Monika

+0

是的,我使用与您提供的代码完全相同的代码,但采用了两种不同的方法。方法1用于发送文件,并在方法2中,我正在执行moveToElement并检索工具提示文本。 – SKV