Selenium 上传文件文件选择不启用提交按钮
Selenium Upload File On file selection doesn't enable submit button
我试过了
File file = new File("/Users/swapnil.kotwal/Desktop/AntVsGradle.jpg");
driver.findElement(By.xpath("//input[@class='libray_create-resource_choose-file_hidden-input']")).sendKeys(file.getAbsolutePath());
我的 HTML 如下所示
<div class="libray_create-resource_choose-file">
<button class="btn is-hollow_blue libray_create-resource_choose-file_button undefined">Choose File</button>
<input type="file" class="libray_create-resource_choose-file_hidden-input">
</div>
<div class="library_create-modal_footer">
<button class="btn is-text_only btn-cancel undefined">Cancel</button>
<button class="btn is-filled_blue undefined" disabled="">Add</button>
</div>
我发现隐藏的 file input
文件路径设置正确。
问题出在 Choose File
按钮元素与文件输入元素不同 //input[@class='libray_create-resource_choose-file_hidden-input']"
似乎有一些 JS 事件使最终 Add
按钮在单击 Choose File
按钮时启用。
所以,我将文件导入文件 HTML 元素,但如何启用 Add
按钮?
我尝试启用该按钮
WebElement yourButton= driver.findElement(By.className("is-filled_blue"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].removeAttribute('disabled','disabled')",yourButton);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(yourButton));
它使该按钮可见但仍然不允许实际单击它。
尝试使用 JavascriptExecutor 来点击它,然后再删除禁用的属性。
WebElement element = driver.findElement(By.xpath("xpath"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
如果您使用 JavascriptExecutor,则无需使其可见
首先让我说 -- 我不在 Java 中编程 -- 但我做在 Python.
中大量 selenium 编码
当我纯粹从 selenium 的角度看这个问题时,我注意到您没有 selecting 选择文件按钮 在发送您的输入之前。我假设你必须 select 这个按钮来启用元素 - btn is-filled_blue undefined
这里有一些伪代码,可能有助于触发该按钮从禁用切换到启用。
File file = new File("/Users/swapnil.kotwal/Desktop/AntVsGradle.jpg");
WebElement select_button = driver.findElement(By.xpath("//*[@class='btn is-hollow_blue libray_create-resource_choose-file_button undefined']"));
select_button.click();
WebElement upload_file = driver.findElement(By.xpath("//input[@class='libray_create-resource_choose-file_hidden-input']"));
upload_file.sendKeys(file.getAbsolutePath());
/* You might need to send an Enter, Return or Tab before the
add_file_button changes. This process requires testing on the website.
upload_file.sendKeys(Keys.ENTER);
upload_file.sendKeys(Keys.RETURN);
upload_file.sendKeys(Keys.TAB);
*/
boolean add_file_button_presence = driver.findElement(By.className("is-filled_blue")).isDisplayed();
boolean add_file_button_enabled = driver.findElement(By.className("is-filled_blue")).isEnabled();
if (add_file_button_presence==true && add_file_button_enabled==true)
{
WebElement add_file_button = driver.findElement(By.className("is-filled_blue"));
add_file_button.click();
}
因为我通常不在 Java 中编程,所以我的伪代码的结构和语法可能不正确。如果是,请告诉我,我将删除此答案或通过一些研究更正问题。
未定义的 class 看起来很可疑 - 尝试通过添加另一个 js 调用来删除它:
js.executeScript("参数[0].style.removeProperty('undefined')",yourButton);
我的建议是 始终将 JavaScript(JavascriptExecutor) 代码的使用限制在模拟最终用户操作而不操纵应用程序行为的范围内
比如启用一个功能被禁用的按钮等
我们的目的是以最终用户使用它的方式模拟应用程序步骤。
我建议使用像AutoIt/Sikuli这样的任何第三方工具来处理这种情况因为发送文件路径不会自动启用'Add'按钮正如预期
1) 使用 Selenium 单击 'Choose File' 按钮打开上传 window,这不是 Web 组件。
2)因为上传 window 不是 Web 组件 Selenium 不支持它。
3) 使用任何第三方工具,如 AutoIt/Sikuli 通过设置文件路径并提交来处理 Windows 上传弹出窗口。
4)当我们以与最终用户相同的方式在 UI 中上传文件时,'Add' 按钮将自动启用。
我设法使用下面的 hack 上传文件...我从来没有点击 Choose File
link/button 启动 Upload File
Windows 弹出窗口和任何意味着 windows 弹出窗口不会消失。
尝试了以下选项。
1.
select_button.click();
select_button.submit();
action.sendKeys(Keys.ESCAPE).perform();
select_button.sendKeys(Keys.ESCAPE);
robot = new Robot();
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);
none 隐藏 Mac OS
上的弹出窗口
所以,我找到了一个 Jugad(#hack 英文)我从来没有点击 Choose File
启动那个 Upload File Window
的按钮
public void uploadFile() {
WebElement element = driver.findElement(By.className("libray_create-resource_choose-file_hidden-input"));
element.sendKeys("/Users/swapnil.kotwal/projectName/automation.pdf");
WebElement addButton = driver.findElement(By.className("is-filled_blue"));
// click was important, thanks to this answer in this thread
driver.executeScript("arguments[0].click();", addButton);
driver.executeScript("arguments[0].removeAttribute('disabled','disabled'); arguments[0].style = \"\"; arguments[0].style.display = \"block\"; " +
"arguments[0].style.visibility = \"visible\";", addButton);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(addButton));
addButton.click();
}
我试过了
File file = new File("/Users/swapnil.kotwal/Desktop/AntVsGradle.jpg");
driver.findElement(By.xpath("//input[@class='libray_create-resource_choose-file_hidden-input']")).sendKeys(file.getAbsolutePath());
我的 HTML 如下所示
<div class="libray_create-resource_choose-file">
<button class="btn is-hollow_blue libray_create-resource_choose-file_button undefined">Choose File</button>
<input type="file" class="libray_create-resource_choose-file_hidden-input">
</div>
<div class="library_create-modal_footer">
<button class="btn is-text_only btn-cancel undefined">Cancel</button>
<button class="btn is-filled_blue undefined" disabled="">Add</button>
</div>
我发现隐藏的 file input
文件路径设置正确。
问题出在 Choose File
按钮元素与文件输入元素不同 //input[@class='libray_create-resource_choose-file_hidden-input']"
似乎有一些 JS 事件使最终 Add
按钮在单击 Choose File
按钮时启用。
所以,我将文件导入文件 HTML 元素,但如何启用 Add
按钮?
我尝试启用该按钮
WebElement yourButton= driver.findElement(By.className("is-filled_blue"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].removeAttribute('disabled','disabled')",yourButton);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(yourButton));
它使该按钮可见但仍然不允许实际单击它。
尝试使用 JavascriptExecutor 来点击它,然后再删除禁用的属性。
WebElement element = driver.findElement(By.xpath("xpath"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
如果您使用 JavascriptExecutor,则无需使其可见
首先让我说 -- 我不在 Java 中编程 -- 但我做在 Python.
中大量 selenium 编码当我纯粹从 selenium 的角度看这个问题时,我注意到您没有 selecting 选择文件按钮 在发送您的输入之前。我假设你必须 select 这个按钮来启用元素 - btn is-filled_blue undefined
这里有一些伪代码,可能有助于触发该按钮从禁用切换到启用。
File file = new File("/Users/swapnil.kotwal/Desktop/AntVsGradle.jpg");
WebElement select_button = driver.findElement(By.xpath("//*[@class='btn is-hollow_blue libray_create-resource_choose-file_button undefined']"));
select_button.click();
WebElement upload_file = driver.findElement(By.xpath("//input[@class='libray_create-resource_choose-file_hidden-input']"));
upload_file.sendKeys(file.getAbsolutePath());
/* You might need to send an Enter, Return or Tab before the
add_file_button changes. This process requires testing on the website.
upload_file.sendKeys(Keys.ENTER);
upload_file.sendKeys(Keys.RETURN);
upload_file.sendKeys(Keys.TAB);
*/
boolean add_file_button_presence = driver.findElement(By.className("is-filled_blue")).isDisplayed();
boolean add_file_button_enabled = driver.findElement(By.className("is-filled_blue")).isEnabled();
if (add_file_button_presence==true && add_file_button_enabled==true)
{
WebElement add_file_button = driver.findElement(By.className("is-filled_blue"));
add_file_button.click();
}
因为我通常不在 Java 中编程,所以我的伪代码的结构和语法可能不正确。如果是,请告诉我,我将删除此答案或通过一些研究更正问题。
未定义的 class 看起来很可疑 - 尝试通过添加另一个 js 调用来删除它: js.executeScript("参数[0].style.removeProperty('undefined')",yourButton);
我的建议是 始终将 JavaScript(JavascriptExecutor) 代码的使用限制在模拟最终用户操作而不操纵应用程序行为的范围内 比如启用一个功能被禁用的按钮等
我们的目的是以最终用户使用它的方式模拟应用程序步骤。
我建议使用像AutoIt/Sikuli这样的任何第三方工具来处理这种情况因为发送文件路径不会自动启用'Add'按钮正如预期
1) 使用 Selenium 单击 'Choose File' 按钮打开上传 window,这不是 Web 组件。
2)因为上传 window 不是 Web 组件 Selenium 不支持它。
3) 使用任何第三方工具,如 AutoIt/Sikuli 通过设置文件路径并提交来处理 Windows 上传弹出窗口。
4)当我们以与最终用户相同的方式在 UI 中上传文件时,'Add' 按钮将自动启用。
我设法使用下面的 hack 上传文件...我从来没有点击 Choose File
link/button 启动 Upload File
Windows 弹出窗口和任何意味着 windows 弹出窗口不会消失。
尝试了以下选项。
1.
select_button.click();
select_button.submit();
action.sendKeys(Keys.ESCAPE).perform();
select_button.sendKeys(Keys.ESCAPE);
robot = new Robot();
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);
none 隐藏 Mac OS
上的弹出窗口所以,我找到了一个 Jugad(#hack 英文)我从来没有点击 Choose File
启动那个 Upload File Window
public void uploadFile() {
WebElement element = driver.findElement(By.className("libray_create-resource_choose-file_hidden-input"));
element.sendKeys("/Users/swapnil.kotwal/projectName/automation.pdf");
WebElement addButton = driver.findElement(By.className("is-filled_blue"));
// click was important, thanks to this answer in this thread
driver.executeScript("arguments[0].click();", addButton);
driver.executeScript("arguments[0].removeAttribute('disabled','disabled'); arguments[0].style = \"\"; arguments[0].style.display = \"block\"; " +
"arguments[0].style.visibility = \"visible\";", addButton);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(addButton));
addButton.click();
}