无法使用 Selenium Webdriver 选择选项
我有很奇怪的问题。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了类似的问题,并且使用 Actions 类获得了更好的结果,然后确保在单击它之前使用 moveToElement() 方法。
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.
The moveToElement method makes sure the element is in the visible window
使用键盘按键我们可以在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);
试试这个
try this