gpt4 book ai didi

php - Selenium click() 有效但 submit() 无效

转载 作者:行者123 更新时间:2023-11-28 03:27:01 25 4
gpt4 key购买 nike

在我的一些 Selinium Webdriver 测试中,我注意到我无法通过调用 submit() 来提交表单。在表单的任何元素上,但我可以调用 click()在提交按钮上就好了。我创建了一个简单的测试网页来说明问题。在下面的示例中,当我调用 submit() 时,我看到提交按钮。但是当我调用 click() ,表单实际上已提交,我看到“已提交”。

<html>
<body>
<?php if(isset($_POST["submit"])): ?>
Submitted
<?php else: ?>
<form method="post">
<input id="submit" name="submit" type="submit" value="submit">
</form>
<?php endif; ?>
</body>
</html>

我正在使用 this Selenium 模板,这是我的测试的样子:

@Listeners(ScreenshotListener.class)
public class LoginTest extends SeleniumBase {
@Test
public void test() {
WebDriver driver = getDriver();
driver.get("http://localhost/test.php");
WebElement submit = driver.findElement(By.id("submit"));
submit.click(); // works
// submit.submit(); // doesn't work
}
}

最佳答案

好的,所以,我有一个类似的问题,我想分享一个迄今为止一直为我服务的解决方案。在某些网页上,我必须使用 .Submit(),而在某些网页上,我必须在按钮元素上使用 .Click()。我必须在其上使用 .Click() 的页面会告诉我输入的用户名或密码不正确;尽管如此,如果我省略 .Submit() 部分并手动单击,它会使用以编程方式填写的凭据登录。所以,我必须通过它的 id 找到它的按钮元素,并在它上面使用 .Click()。第二个网站的行为完全相反,当 .Click() 在按钮上使用时,它什么也不做。相反,.Submit() 有效。

所以,为了解决这个问题直到我弄清楚(我已经研究了一段时间),这就是我所做的。大多数网站在 Enter 键按下事件上执行提交,所以,这正是我所做的。

这是我的代码片段:

private void Login(string url, string usernameElement, string username, 
string passwordElement, string password)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl(url);
IWebElement query = driver.FindElement(By.Id(usernameElement));
query.SendKeys(username);
query = driver.FindElement(By.Id(passwordElement));
query.SendKeys(password);
query.SendKeys(Keys.Enter); // simulates Enter key down to submit
}

这个小改动让我省去了很多麻烦。我已经在其他几个网站上对其进行了测试,到目前为止没有任何问题。我知道这不是库问题的完整修复,但它是A 修复。

关于php - Selenium click() 有效但 submit() 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20587449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com