如何在Java中使用Selenium提交表单

时间:2018-12-23 10:05:54

标签: java selenium selenium-webdriver selenium-chromedriver

我正在使用{ $addFields: { convertedId: { $map: { input: "$request_by", as: "r", in: { $toObjectId: "$$r.userId" } } } } } 使系统自动化。语言是Selenium WebDriver。我已经完成了大多数事情,但是我只停留在一点。我需要提交一个包含用户名,密码和重新输入密码字段的表格。但是,当我用硒填充它们并单击“创建帐户”按钮时,它不起作用。

  

这是代码,

Java
  

我使用这些方法提交了表单。但是他们都不起作用。

    System.setProperty("webdriver.chrome.driver", "./ChromeDriver/chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    driver.get("URL Here");

    WebElement element9 = driver.findElement(By.id("accountname"));
    element9.sendKeys(username);

    WebElement element10 = driver.findElement(By.id("password"));
    element10.sendKeys(password);

    WebElement element12 = driver.findElement(By.id("reenter_password"));
    element12.sendKeys(password);
  

表单HTML来自Inspect元素,

    element12.sendKeys(Keys.ENTER); //method 1

    WebElement element11 = driver.findElement(By.linkText("Complete Sign-up"));
    element11.click(); //method 2

    ((JavascriptExecutor)driver).executeScript("javascript:CompleteCreateAccount();"); //method 3

    WebElement element13 = driver.findElement(By.xpath("//a[@id='createAccountButton']/span"));
    element13.click(); //method 4

    element13.submit(); //method 5

    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", element13); //method 6

  

另外,我还需要说一下,当我手动单击<div class="leftcol"> <div id="error_display" style="display: none;"></div> <div class="checkout_content_box" id="cart_area"> <form id="create_account" name="create_account" action="" onsubmit="CompleteCreateAccount(); return false; " method="POST"> <input type="hidden" name="lt" id="lt" value="0"> <div id="account_form_box"> <div class="join_form"> <div class="form_row"> <div class="form_area"> <label for="accountname">Create a Steam account name</label> <input type="text" maxlength="64" name="accountname" id="accountname" onchange="CheckAccountNameAvailability();"> </div> <div class="form_notes"> <br> <span class="input_note"><a href="javascript:CheckAccountNameAvailability();">Check availability</a></span> <span id="accountname_availability" style="display: none;"></span> </div> <div style="clear: left;"></div> </div> <div id="form_row_choose_suggested_name" class="form_row" style="display: none;"> <div class="form_area"> <label for="choose_accountname">Available account names</label><br> <select id="choose_accountname" name="choose_accountname" size="3" onchange="UpdateAccountName( this.value );"> <option id="suggested_name_1"></option> <option id="suggested_name_2"></option> <option id="suggested_name_3"></option> </select> </div> <div class="form_notes"> <br> <span class="choose_accountname_description">Select one or try again with another name of your choice</span> </div> <div style="clear: left;"></div> </div> <div class="rule"></div> <div class="form_row"> <div class="form_area"> <label for="password">Choose a password</label> <input type="password" maxlength="64" name="password" id="password" onkeyup="CheckPasswordStrength()" autocomplete="off"> </div> <div class="form_notes"> <div id="password_tag" class="password_tag">&nbsp;</div> </div> <div style="clear: left;"></div> </div> <div class="form_row"> <div class="form_area"> <label class="reenter_row" for="reenter_password">Re-enter password</label> <input type="password" maxlength="64" name="reenter_password" id="reenter_password" onkeyup="ReenterPasswordChange()" autocomplete="off"> </div> <div class="form_notes"> <br> <div id="reenter_tag" class="password_tag">&nbsp;</div> </div> <div style="clear: left;"></div> </div> <div class="rule"></div> <div class="form_row"> <div id="priv_and_sub"> <a id="createAccountButton" class="btnv6_blue_hoverfade btn_medium" href="javascript:CompleteCreateAccount();" style="float: right;"> <span>Complete Sign-up</span> </a> <div style="clear: right;"></div> </div> <div id="ssa_submit"> </div> </div> </div> </div> </form> </div> 时,它正在工作。但是使用硒无法正常工作并传递此消息

     

您选择的帐户名不可用。请选择   另一个名字。您输入的密码是不允许的。请选择一个   不同的密码,至少8个字符。

     

但是用户名和密码可以。没有什么不对。因为当我   手动单击它正在使用该用户名和密码。

2 个答案:

答案 0 :(得分:2)

过去我也遇到过类似的问题,有时它有助于将重点放在其他元素上,然后单击...

我将首先尝试将注意力集中在<div id="ssa_submit"></div>上。

编辑:

您说手动操作有效,因此我建议您像这样使用Action

Actions action = new Actions(driver);
WebElement btnElement=driver.findElement(By.xpath("//a[@id='createAccountButton']"));
action.doubleClick(btnElement).build().perform();

但这可能是您确实在系统中存在一个错误...

编辑2:

硒的最佳实践是将WebDriverWaitExpectedConditions一起使用:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.elementToBeClickable(By.id("createAccountButton")));

请参见ExpectedConditionsWebDriverWait

我认为这应该可以完成工作!

希望这对您有所帮助。

答案 1 :(得分:1)

不要单击By.xpath("//a[@id='createAccountButton']/span")并提交表单,而是单击用户名,密码或确认密码字段,然后按Enter。

 System.setProperty("webdriver.chrome.driver", "./ChromeDriver/chromedriver.exe");

 WebDriver driver = new ChromeDriver();
 driver.get("URL Here");

 WebElement element9 = driver.findElement(By.id("accountname"));
 element9.sendKeys(username);

 WebElement element10 = driver.findElement(By.id("password"));
 element10.sendKeys(password);

 WebElement element12 = driver.findElement(By.id("reenter_password"));
 element12.sendKeys(password);

 element12.click();
 element12.submit();