我遇到了Selenium的一些问题。 我有这个叠加div,它包含一个输入类型文件,我试图选择它。
<div class="xenOverlay attachmentUploader" style="z-index: 9999; top: 92.7px; left: 206px; position: fixed; display: block;"><form action="http://localhost/xenforo/index.php?attachments/do-upload" method="post" enctype="multipart/form-data" class="formOverlay AutoInlineUploader AttachmentUploadForm NoAutoHeader" data-overlayclass="attachmentUploader">
<dl class="ctrlUnit">
<dt><label for="ctrl_upload">Upload a File (<span title="1,048,576 bytes">Max 1 MB</span>):</label></dt>
<dd id="SWFUploadContainer" data-placeholder="#SWFUploadPlaceHolder" data-trigger="#ctrl_upload">
<span id="SWFUploadPlaceHolder"></span>
<input type="file" name="upload" class="textCtrl" onchange="this.blur()" id="ctrl_upload">
<input type="reset" value="Close" class="OverlayCloser button smallButton">
</dd>
</dl>
<div class="attachmentConstraints pairsRows">
<dl><dt>Accepted file types:</dt> <dd>zip, txt, pdf, png, jpg, jpeg, jpe, gif, torrent</dd></dl>
</div>
<input type="hidden" name="_xfToken" value="1,1482434233,ccadf23a674c4c4b9def910cf5d55cea4b44ab2a">
<input type="hidden" name="hash" value="a9288bd75c0c87638d9057237511e16c">
<input type="hidden" name="content_type" value="post">
<input type="hidden" name="key" value="">
<input type="hidden" name="content_data[node_id]" value="3">
我尝试按id和xpath选择输入类型文件,但eclipse只给我错误。
WebElement elem = driver.findElement(By.xpath("//*[@id='ctrl_upload']"));
elem.sendKeys("C://e.txt");
我在Windows 10上使用Selenium 3.0.1和ChromeDriver
更新:
完整代码:
static void createNewThread(WebDriver驱动程序,字符串类别,字符串标题,字符串消息){
String url = "http://localhost/xenforo/index.php?forums/" + category + "/create-thread";
driver.get(url);
WebElement ttl = driver.findElement(By.className("titleBOT"));
ttl.clear();
ttl.sendKeys(title);
WebElement msg = driver.findElement(By.xpath("//*[@id='ThreadCreate']/fieldset[1]/dl[2]/dd/div/div/iframe"));
msg.click();
msg.clear();
msg.sendKeys(message);
driver.findElement(By.xpath("//*[@id='ctrl_uploader']")).click();
WebElement elem = driver.findElement(By.xpath("//input[@type='file']"));
elem.sendKeys("C:\\e.txt");
//driver.findElement(By.xpath("//*[@id='ThreadCreate']/dl[2]/dd/input[1]")).click();
}
我认为它找不到我正在搜索的元素。仅当我单击按钮并覆盖旧内容时,才会显示此元素。
更新2: 我想我已经找到了问题。上传是通过flash,而不是通过html,当我通过“click()”点击flash按钮时,网站对真实用户点击的反应不同。
答案 0 :(得分:0)
请尝试以下操作:
WebElement elem = driver.findElement(By.xpath("//input[@type='file']"));
elem.sendKeys("C://e.txt");
请告诉我,是否有效。
答案 1 :(得分:0)
试试这个简单的方法希望它能起作用 -
// Set a simple gesture to handle our click request, allowing everything else
final GestureDetector.SimpleOnGestureListener dsl_simpleDetector = new GestureDetector.SimpleOnGestureListener() {
public boolean onSingleTapUp (MotionEvent motionEvent) {
// Don't want to do anything with this if timer is running
final ExchangeEmailActivity ds_act = (ExchangeEmailActivity) getActivity();
if (ds_act.dsc_timer != null) { // We're waiting for the page to load still
return false; // Handle it by doing nothing
} else {
return true; // Let it be handled as necessary
}
}
};
// The listener will call the simple gesture above to handle it
vp_pager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return (dsl_simpleDetector.onSingleTapUp(motionEvent));
}
});
并将文件路径driver.findElement(By.id("ctrl_upload")).sendKeys("C:\\e.txt");
更正为C://e.txt