无法使用 Selenium Webdriver 选择选项

发布于 2024-11-18 23:27:15 字数 1297 浏览 1 评论 0原文

我有很奇怪的问题。我正在使用 selenium 编写简单的网络机器人,尝试用数据填充页面、提交数据并收获结果。

我填写了所有表格,没有任何问题,但是我必须首先输入邮政编码,然后单击其他位置以便 AJAX 列出所有可能性,然后选择适当的选项(我想始终选择第一个选项)。

但我的问题是,我根本无法选择它。我填写 ZIP,单击选项列表本身,等待“请选择”消息消失(此时我的选择应该在那里),然后选择它。我尝试了 option.click(),尝试了 selectByVisibleText(),甚至尝试了已弃用的 setSelected()。什么也没发生。我在 FF 中看到的只是选项的下拉列表,第一个被标记,但仅此而已。我尝试了很多方法,但一点运气都没有。

这是我最后一次尝试的代码:

ZIPCode = driver.findElement(By.id("formparam_data2_zip")); //get and fill ZIP
ZIPCode.sendKeys(ZIP);
address = driver.findElement(By.name("formparam_data2_zip_id")); // click to fire AJAX
address.click();
(new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() { 
public Boolean apply(WebDriver d) {                    // wait until AJAX shows results
WebElement elm = d.findElement(By.id("formparam_data2_zip_id"));
List<WebElement> options = elm.findElements(By.tagName("option"));
for(WebElement w : options){
if(w.getText() != "Prosím, vyberte."){
return true;
}}
return false;
}});
List<WebElement> options = address.findElements(By.tagName("option"));   
options.get(0).click(); // click first option - ! this failes  !
phaseTwoBtn = driver.findElement(By.id("formparam_data2_next")); // than submit...
phaseTwoBtn.submit();

I have very strange problem. Using selenium I am writing simple web-bot trying to fill page with data, submit them, and harvest results.

I fill all the forms with no problems at all, but than I have to first enter the ZIP code, than click somewhere else for AJAX to list all the possibilities, than select the appropriate option (I want to always select the first one).

But my problem is, I simply can't select it. I fill the ZIP, click the option list itself, wait to "please select" message to get lost (by this time my choice should be there) and than selecting it. I tried option.click(), I tried selectByVisibleText(), and even the deprecated setSelected(). Nothing happens. All I see in FF is drop-downed list of option, with the first one being marked, but that's all. I tried many ways, with no luck at all.

There is my last-attempt code:

ZIPCode = driver.findElement(By.id("formparam_data2_zip")); //get and fill ZIP
ZIPCode.sendKeys(ZIP);
address = driver.findElement(By.name("formparam_data2_zip_id")); // click to fire AJAX
address.click();
(new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() { 
public Boolean apply(WebDriver d) {                    // wait until AJAX shows results
WebElement elm = d.findElement(By.id("formparam_data2_zip_id"));
List<WebElement> options = elm.findElements(By.tagName("option"));
for(WebElement w : options){
if(w.getText() != "Prosím, vyberte."){
return true;
}}
return false;
}});
List<WebElement> options = address.findElements(By.tagName("option"));   
options.get(0).click(); // click first option - ! this failes  !
phaseTwoBtn = driver.findElement(By.id("formparam_data2_next")); // than submit...
phaseTwoBtn.submit();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只是我以为 2024-11-25 23:27:15

我遇到了类似的问题,并且使用 Actions 类获得了更好的结果,然后确保在单击它之前使用 moveToElement() 方法。

Actions builder = new Actions(d);


builder.moveToElement(options.get(0)));
builder.click();
builder.build().perform();

moveToElement 方法确保元素位于可见窗口中

I had a similar problem and had better results using the Actions class and then making sure to use the moveToElement() method before clicking on it.

Actions builder = new Actions(d);


builder.moveToElement(options.get(0)));
builder.click();
builder.build().perform();

The moveToElement method makes sure the element is in the visible window

裂开嘴轻声笑有多痛 2024-11-25 23:27:15

使用键盘按键我们可以在selenium webdriver中解决这个问题。上面例子的代码为,ZIPCode.sendkeys (ZIP);ZIPCode.sendkeys (Keys.Tab);
ZIPCode.sendkeys(Keys.Return);

Using key board keys we can solve this problem in selenium webdriver .Code for the above example,ZIPCode.sendkeys (ZIP);ZIPCode.sendkeys (Keys.Tab);
ZIPCode.sendkeys (Keys.Return);

巴黎夜雨 2024-11-25 23:27:15

试试这个

if(!w.getText().equals("Prosím, vyberte.")){
    return true;
}

try this

if(!w.getText().equals("Prosím, vyberte.")){
    return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文