java Selenium 找不到输入类型提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25281161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium cannot find an Input type submit
提问by spidee76
I have a form that contains some input types 'text' which selenium finds and populates ok. However it cannot find the input type submit on form, the structure looks somelike this this:
我有一个包含一些输入类型“文本”的表单,selenium 可以找到并填充它。但是它找不到表单上提交的输入类型,结构看起来像这样:
<div>
<div>
<form>
<div>other elements are here....</div>
<p> <input class="btn btn-success" type="submit" name="yt0" value="Register Now" /></p>
</form>
</div>
<div>
I have tried a few methods the latest of which is:
我尝试了几种方法,其中最新的是:
driver.findelement(By.name("yt0")).click();
also tried:
也试过:
driver.findelement(By.name("yt0")).submit();
I can see the element clearly on the page, and selenium has entered all the text needed for the elements above the submit.
我在页面上可以清楚地看到元素,并且selenium已经输入了submit上方元素所需的所有文本。
Is it possible that Selenium cannot find the submit because its contained in a hierarchy of DIVs? Thanks
Selenium 是否有可能无法找到提交,因为它包含在 DIV 的层次结构中?谢谢
回答by QAMate.com
Form is the one which has to be submitted.
表格是必须提交的。
Use
利用
driver.findElement(By.tagName("form")).submit();
Hope it helps.
希望能帮助到你。
回答by Paul Harris
Selenium should be fine to find the above element, from the information you have provided I cannot see why it would not be able to click it. Have you tried a different locator?
Selenium 应该可以找到上述元素,根据您提供的信息,我看不出为什么它无法单击它。您是否尝试过不同的定位器?
One example would be:
一个例子是:
driver.findElement(By.className("btn-success")).click();
or maybe you can look at others here
或者你可以在这里看看其他人
There are a few other things that could be happening is the button disabled when selenium clicks it or the button appears only once the form is filled in? If so you will need to add a fluent waitfor it to become enabled (or use a thread.sleep(500) to test it works before spending the time on the wait).
还有一些其他可能发生的事情是当 selenium 单击它时按钮被禁用还是只有在填写表单后才会出现按钮?如果是这样,您将需要添加流畅的等待以使其启用(或在等待之前使用 thread.sleep(500) 来测试它是否有效)。
回答by user2719138
Your Code looks fine. But please check if the submit is hidden if the element is hidden we have to use Javascript to click on the hidden element. But before that please try using a different locator instead of name try xpath.
你的代码看起来不错。但是请检查提交是否隐藏如果元素隐藏我们必须使用Javascript点击隐藏元素。但在此之前,请尝试使用不同的定位器而不是名称尝试 xpath。
回答by cL83
Another possibility is the element appear in DOM however it is not clickable yet. Perhaps your can try study and implement explicit wait.
另一种可能性是该元素出现在 DOM 中,但尚不可点击。也许你可以尝试学习并实现显式等待。
Here are a good samples of WaitTools.java
这里有一个很好的WaitTools.java 示例
Hope this helps :D
希望这会有所帮助:D
回答by broot02
Selenium often is faster than the page loading, say for example you clicked a link and it takes you another page. There is not always an implicit wait, maybe you need to wait for element to become visible.
Selenium 通常比页面加载更快,例如,您单击一个链接,然后它会带您进入另一个页面。并不总是隐式等待,也许您需要等待元素变为可见。
This can be done with Explicit waits, see code below.
这可以通过Explicit waits来完成,参见下面的代码。
import import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
WebDriverWait wait = new WebDriverWait(driver, seconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By
.className(className)));
Another option is to use JavaScriptExecutor, if the Input
is hidden this can help use Javascript to change the attributes.
另一种选择是使用JavaScriptExecutor,如果Input
隐藏,这可以帮助使用 Javascript 更改属性。
Remember that sometimes you need to wait for an element, and it is a good idea to have a helper class with static methods so you can easily call these methods, without rewriting the code, here is a sample method.
请记住,有时您需要等待一个元素,最好有一个带有静态方法的辅助类,这样您就可以轻松调用这些方法,而无需重写代码,这里有一个示例方法。
public static void waitForVisibilityByClass(WebDriver driver,
String className, int seconds) {
WebDriverWait wait = new WebDriverWait(driver, seconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By
.className(className)));
}
Let me know if this doesn't help.
如果这没有帮助,请告诉我。