我正在使用Selenium WebDriver来自动化系统.语言是Java.我已经完成了大多数事情,但是我只停留在一点.我需要提交一个包含用户名,密码和重新输入密码字段的表格.但是当我填充它们(使用硒)并单击“创建帐户”按钮时,它不起作用.
Here is the code,
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);
I used these methods to submit the form. But none of them worked.
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
Form HTML got from Inspect Element,
And also I need to say when I click Create Account button manually it is working. But using selenium It is not working and pass this message
The account name you have chosen is not available. Please choose
another name. The password you entered is not allowed. Please select a
different password, with at least 8 characters.
But username and passwords are okay. Nothing wrong. Because when I
click it manually It is working with that username and password.
解决方法:
过去我遇到过类似的问题,有时它有助于将重点放在其他元素上,然后单击…
我将从尝试着眼于< 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:
Selenium中的最佳实践是将WebDriverWait与ExpectedConditions结合使用:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.elementToBeClickable(By.id("createAccountButton")));
我认为这应该做的!
希望这对您有所帮助.
标签:selenium,selenium-webdriver,selenium-chromedriver,java
来源: https://codeday.me/bug/20191108/2007285.html