java selenium怎么用_如何在Java中使用Selenium提交表单

我正在使用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,

Create a Steam account name

Check availability

Available account names

Select one or try again with another name of your choice

Choose a password

Re-enter password

Complete Sign-up

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值