无法使用Selenium WebDriver 2.31(JRE 7)定位元素
我正在尝试使在网站上预订公交车票的场景自动化.我在Eclipse中使用了Selenium WebDriver,当我尝试查找元素(即乘客姓名")时,没有编译错误,但是在执行时显示了诸如"Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//input[@name='i_passengerAge']"}
"的错误.
I'm trying to automate a scenario which books a bus ticket in a website. I'm using Selenium WebDriver with Eclipse and when i try to locate the element, i.e. 'Passenger name', no compilation errors, but while executing it shows an error such as "Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//input[@name='i_passengerAge']"}
".
下面的HTML和Java代码,我也需要一个快速的解决方案.
HTML and Java code below, also i need an hasty solution for this.
我的系统信息:
- Windows 7
- Selenium WebDriver 2.31
- Eclipse FW
网页的HTML:
<input name="i_passengerName" id="i_passengerName" maxlength="30" class="inputclass pageRequired commonInputStyle" title="Please enter your name!" type="text">
<input name="i_passengerAge" id="i_passengerAge" maxlength="2" size="4" class="inputclass fillAge digits commonInputStyle" type="text">
我的自动化脚本:
WebElement PD_Name = driver.findElement(By.name("i_passengerName"));
PD_Name.sendKeys(new String[] {"Testing"});
PD_Name.submit();
WebElement PD_Age = driver.findElement(By.name("i_passengerAge"));
PD_Age.sendKeys(new String[] {"45"});
PD_Age.submit();
我希望您尝试使用By.id
,By.cssSelector
或By.xpath
来获取元素.
I hope you've tried to get the element with By.id
, By.cssSelector
or By.xpath
.
当心submit()
方法.因为它会自动搜索要发送的表单,有时可能会失败.这就是为什么我更喜欢老式的click()
.
Watch out about the submit()
method. Because it search automaticaly the form to send and sometimes it may fail. That's why i prefere the good old click()
.
By.id:
WebElement PD_Name=driver.findElement(By.id("i_passengerName"));
PD_Name.sendKeys("Testing");
PD_Name.click();
通过.xpath:
WebElement PD_Name=driver.findElement(By.xpath("//input[@name='i_passengerName']"));
PD_Name.sendKeys("Testing");
PD_Name.click();
对于cssSelector方式,您可以此处.
For the cssSelector way, you can take a look here.
告诉我是否有所改善. =)
Tell me if it improved. =)
实际上,没有 i_passengerGender