我需要通过Selenium webdriver上传文件。 但如果我使用类似的东西:
driver.findElement(By.xpath("//input[@type='file']")).sendKeys(file.getAbsolutePath());
然后我发现了一个错误:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 128 milliseconds
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_91'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
所以我想我需要使用js删除Hidden属性,我找到了这段代码:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementsByTagName('*')[0].removeAttribute('hidden');");
但它不适合我。
这是我正在处理的代码:
<label name="file" ng-model="file" ngf-accept="pattern" ngf-pattern="pattern" ngf-select="uploadSubmit($file)" ng-disabled="isUploadDisabled()" type="button" class="button button--large ng-pristine ng-untouched ng-valid ng-empty">
<span translate="" class="button__text">Загрузить из файла .xls</span>
<input type="file" hidden="" ng-disabled="isUploadDisabled()">
</label>
答案 0 :(得分:0)
这应该有效:
试一试:
console.log(document.getElementsByTagName("input")[0].removeAttribute('hidden'));
或强>
if (document.all !== undefined)
{
var allElements = document.all;
}
else
{
var allElements = document.getElementsByTagName("*");
}
allElements[0].removeAttribute('hidden');
答案 1 :(得分:0)
我建议不要使用JS方法通过代码删除function imagecreatefromfile( $filename ) {
if (!file_exists($filename)) {
throw new InvalidArgumentException('File "'.$filename.'" not found.');
}
switch ( strtolower( pathinfo( $filename, PATHINFO_EXTENSION ))) {
case 'jpeg':
case 'jpg':
return imagecreatefromjpeg($filename);
break;
case 'png':
return imagecreatefrompng($filename);
break;
default:
throw new InvalidArgumentException('File "'.$filename.'" is not valid jpg or png.');
break;
}
}
/* do resizing of picture */
$src = imagecreatefromfile($target);
$path1 = strtolower( pathinfo( $target, PATHINFO_EXTENSION ));
list ($width, $height) = getimagesize($target);
$newwidth = 300;
if($heigth > $width){
$newheight = ($height/$width)*$newwidth;
}
else if($width == $height){
$newheight = 300;
}
else if($width > $height){
$newheight = ($width/$height)*$newwidth;
}
unlink($target);
switch ($path1)
{
case 'jpg':
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($tmp, $target, 90);
imagedestroy($src);
imagedestroy($tmp);
break;
case 'png':
$backgroundImg = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($backgroundImg, 255, 255, 255);
imagefill($backgroundImg, 0, 0, $color);
imagecopy($backgroundImg, $src, 0, 0, 0, 0, $width, $height);
$tmp2 = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp2, $backgroundImg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($tmp2, $target, 90);
imagedestroy($src);
imagedestroy($backgroundImg);
imagedestroy($tmp2);
break;
}
?>
属性,因为Selenium将用于模拟实际的用户操作。
就像用户点击任何按钮并且网站显示hidden
一样。我建议你先触发相同的组件,然后在显示后使用隐藏的组件。
答案 2 :(得分:0)
首先尝试添加一些等待时间,以便可以在DOM中加载元素。 否则,请尝试使用以下代码使元素可见。
WebElement element = yourWebDriverInstance.findElement(Locator);
((JavascriptExecutor) yourWebDriverInstance).executeScript(arguments[0].style.height='auto'; arguments[0].style.visibility='visible';, element);