我正在尝试点击/发送键到网页上标有Browse
的按钮,无论我尝试什么,Selenium都找不到它。
使用Selenium的IDE,我可以收集以下信息:
id=uploadField
name=uploadField
css=#uploadField
dom:name = document.uploadForm.uploadField
xpath:attributes = //input[@id='uploadField']
xpath:idRelative = //div[@id='browseBtnContainer']/form/input[4]
dom:index = document.uploadForm.elements[3]
xpath:position = //input[4]
我已经尝试了以下所有的By。序列,所有这些都抛出NoSuchElementException
。只有一个iframe,我试图切换到iframe但它没有用。
WebElement browse = driver.findElement(By.id("uploadField"));
WebElement browse1 = driver.findElement(By.name("uploadField"));
WebElement browse2 = driver.findElement(By.cssSelector("#uploadField"));
WebElement browse3 = driver.findElement(By.cssSelector("uploadField")); // just in case
WebElement browse4 = driver.findElement(By.xpath("uploadField")); // attributes
WebElement browse5 = driver.findElement(By.xpath("//div[@id='browseBtnContainer']/form/input[4]")); // idrelative
WebElement browse6 = driver.findElement(By.xpath("//input[4]")); // position
代码的容器部分让我困惑。我不知道如何访问容器中的元素,我在谷歌上找到的任何东西都告诉我使用xpath,我似乎无法找到它。
我是否错误地使用了By.xpath?如何生成网页上每个元素的列表? 以下是相关的HTML源代码:
<div id="ext-comp-1022" class=" x-panel x-border-panel" style="left: 0px; top: 0px; width: 1077px;">
<div id="ext-gen27" class="x-panel-bwrap">
<div id="ext-gen28" class="x-panel-body x-panel-body-noheader" style="overflow: auto; width: 1075px; height: 502px;">
<div id="ext-comp-1023" class=" x-panel x-panel-noborder">
<div id="stepOnePanel"/>
<div id="stepTwoPanel"/>
<div id="stepThreePanel"/>
<div id="stepOnePanel" class="step-container" style="margin:20px 20px 20px 30px;">
<div class="step-title" style="background-color:transparent;background-repeat:no-repeat;background-position:top left;background-image:url(/assets/icons/medium/icon-1.png );padding:12px 0px 0px 50px;height:30px;font-weight:bold;">Start by selecting the ZIP file which contains your images (20MB max)</div>
<div id="ext-gen71" style="padding-left:70px;overflow:auto;">
<div id="ext-comp-1024" class=" x-panel x-panel-noborder">
<div id="ext-gen74" class="x-panel-bwrap">
<div id="ext-gen75" class="x-panel-body x-panel-body-noheader x-panel-body-noborder">
<div style="padding-top:2px">
<div id="browseBtnContainer" style="float:left;width:85px;margin-top:-2px">
<table id="browseBtn" class="x-btn stepBtn x-btn-noicon x-btn-over" cellspacing="0" style="width: 79px;">
<form enctype="multipart/form-data" target="hiddenUploadFrame" method="post" action="/uploader/upload?actingAsUserId=pleach¤tAccountId=bmwofreading&uploadAccountId=bmwofreading&ccmode=multiwindow" name="uploadForm">
<input id="doNotResize" type="checkbox" style="display:none" value="true" name="doNotResize"/>
<input id="pdfConvert" type="checkbox" style="display:none" value="true" name="pdfConvert"/>
<input type="hidden" value="false" name="previewNeeded"/>
<input id="uploadField" type="file" size="1" style="position: absolute; top: 0px; left: -19px; opacity: 0; cursor: pointer; height: 22px;" name="uploadField"/>
答案 0 :(得分:0)
尝试以下方法。
driver.findElement(By.xpath("//input[@id='uploadField']")).click();
答案 1 :(得分:0)
根据提供的HTML,我们可以使用id或name直接尝试。如果您只查找xpath,请尝试以下相对xpath
//input[@id='uploadField']
如果直接点击不起作用,请尝试使用操作。首先移动到元素然后尝试单击。我希望这种方式在大多数情况下对我有帮助并且对我有益
Actions move=new Actions(driver);
move.moveToElement(driver.findElement(By.xpath("//input[@id='uploadField']"))).click().build().perform();
最后你可以尝试使用javascript executor
WebElement element = driver.findElement(By.id("uploadField"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
谢谢你, 穆拉利
答案 2 :(得分:0)
您需要切换到frame
才能与其中的元素进行互动
driver.switchTo.frame("id"); //using the frame id attribute
// or
driver.switchTo.frame("name"); //using the frame name attribute
// or
WebElement frame = driver.findElement(...);
driver.switchTo.frame(frame); //using the frame as WebElement
并切换回来
driver.switchTo().defaultContent();