网络驱动程序HtmlUnitDriver + Java +下拉
我是这个论坛的新手,对 Selenium 及其周围的一切也有点陌生。我对Java有点熟悉,但我当然不是专业人士
我知道有这样的线程,但我仍然没有找到我的答案...... 因为其中一些是用 C# 或 .NET 编写的,...。
我使用 Webdriver 和 HtmlUnitDriver 因为我不希望 Selenium 弹出浏览器。 一切都必须在后台完成(不想看到任何东西,只想看到 1 个结果(如果一切顺利与否))。
好了,话已经说了,情况是这样的。 我正在检查一个网站,有时我需要从下拉列表中选择一个选项(假设是第二个选项)。 但问题是这行不通...
这是我正在使用的一些代码:
public class LoginLogout implements SeleniumTest{
private WebDriver webDriver;
@Override
public void setUp(String baseURL){
webDriver = new HtmlUnitDriver();
}
@Override
public void invoke(){
// Login
webDriver.get("http://website");
webDriver.findElement(By.name("username")).sendKeys("Dummy123456");
webDriver.findElement(By.name("password")).sendKeys("Muddy");
webDriver.findElement(By.className("Submit")).click();
webDriver.findElement(By.name("/catalogue")).click();
webDriver.findElement(By.name("/catalogue/search/synthesis/s10")).click();
webDriver.findElement(By.name("firstYear")).findElement(By.name("2008")).isSelected();
webDriver.findElement(By.name("lastYear")).findElement(By.name("2008")).isSelected();
webDriver.findElement(By.className("Submit")).click();
webDriver.findElement(By.className("Label")).click();
}
@Override
public void tearDown(){
webDriver.close();
}
}
在我选择 FIRSTYEAR 和 LASTYEAR 的地方,这就是出错的地方...
有人可以帮我吗! 如果我能让这个工作(在你的帮助下)就太好了
提前致谢
I'm new to this forum and i'm kinda new to Selenium and everything around it. I'm a little familiar with Java, but i'm certainly not a pro
I know that there a threads like these, but still i haven't found my answer yet...
Because some of them are in C# or .NET, ... .
I'm using Webdriver and HtmlUnitDriver because i don't want Selenium to pop up a browser.
Everything has to be done in the background (don't wanna see anything, only 1 result (if everything went fine or not)).
Okay, that been said, here is the situation.
I'm checking a website, and at some point i need to select an option from a drop down list (let's say the second option).
But the problem is that this won't work...
Here is some code i'm using:
public class LoginLogout implements SeleniumTest{
private WebDriver webDriver;
@Override
public void setUp(String baseURL){
webDriver = new HtmlUnitDriver();
}
@Override
public void invoke(){
// Login
webDriver.get("http://website");
webDriver.findElement(By.name("username")).sendKeys("Dummy123456");
webDriver.findElement(By.name("password")).sendKeys("Muddy");
webDriver.findElement(By.className("Submit")).click();
webDriver.findElement(By.name("/catalogue")).click();
webDriver.findElement(By.name("/catalogue/search/synthesis/s10")).click();
webDriver.findElement(By.name("firstYear")).findElement(By.name("2008")).isSelected();
webDriver.findElement(By.name("lastYear")).findElement(By.name("2008")).isSelected();
webDriver.findElement(By.className("Submit")).click();
webDriver.findElement(By.className("Label")).click();
}
@Override
public void tearDown(){
webDriver.close();
}
}
Where i choose the FIRSTYEAR and LASTYEAR, that's where it's going wrong...
Can someone please help me out !!!
It would be great that i get this to work (with your help)
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
isSelected() 只会告诉您该元素是否已被选择,它实际上不会设置任何内容。我相信您想要做的是将 WebElement 转换为 Select 对象,该对象提供选择选择框项目的方法。
您还可以按索引进行选择,在我实际上并不关心选择哪个值但想要确保选择某些值的情况下,我发现索引更可靠。
isSelected() will only tell you if the element is already selected or not, it won't actually set anything. I believe what you want to do is cast the WebElement to a Select object which provides methods for choosing select box items.
You can also select by Index which I find more reliable in cases where I don't actually care which value is selected but want to ensure something is chosen.